[英]Using Jquery $When to make async and sync function calls
我需要進行異步調用和同步調用,並且當兩個調用都結束時,我需要調用另一個函數。 為此,我正在使用JQuery。 當我進行多個異步調用時,此功能運行良好,但是當我進行同步調用時,出現錯誤。 我正在使用以下代碼進行呼叫。
var requestTwo = function() {
var v = 10;
var j = 20;
var k = v + j;
};
$.when($.get(url, function (result, status, xhr) {
myresult = result;
})),
(requestTwo).then(function (r1, r2) {
//DO something
});
我收到以下錯誤:
JavaScript runtime error: Object doesn't support property or method 'then'
您不需要$.when
進行同步函數調用時,只需調用它們
$.get(url, function (result, status, xhr) {
var myresult = result;
requestTwo();
// DO something here, it's synchronous and single threaded,
// so the function will be finished
});
據我了解,您對“何時”功能的第二種說法必須是一個承諾。 在此處查看http://learn.jquery.com/code-organization/deferreds/#promises 。 這似乎是您的代碼中的錯字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.