我有以下代碼,我想異步運行,但日誌顯示它們正在同步運行。JQuery AJAX請求表現爲同步不明原因
$(".widget").each(function() {
// snip - url, def, and tempParams defined here
$.ajax({
url: url,
dataType: "jsonp",
type: "POST",
data: {params: tempParams},
success: function(data) {
var st = new Date().getTime();
console.error("Start " + def.name + " @" + st);
doSomething(data);
var et = new Date().getTime();
console.error("End " + def.name + " @" + et);
console.error("Duration " + def.name + " " + (et-st) + " ms.");
}
});
控制檯輸出清楚地顯示了成功職能,以便執行,而不是異步:
Start f1 @1300738891705
End f1 @1300738891744
Duration f1 39 ms.
Start f2 @1300738892746
End f2 @1300738893280
Duration f2 534 ms.
Start f3 @1300738893282
End f3 @1300738893303
Duration f3 21 ms.
Start f4 @1300738893305
End f4 @1300738893306
Duration f4 1 ms.
Start f5 @1300738893484
End f5 @1300738895609
Duration f5 2125 ms.
的思考?我覺得這一定是我忽略的事情。
我知道這是愚蠢的。我在ajax調用之外添加了調試代碼(就像我應該做的那樣),它們確實是異步執行的。我正在想象他們會在他們自己的線程中執行,這導致了我對錯誤的理解。感謝您清理東西! – 2011-03-21 21:01:07