等待jQuery的Ajax响应(S) [英] Waiting for jQuery AJAX response(s)
问题描述
我有,使用jQuery阿贾克斯被称为100倍(异步:真)页面,问题是,它们都被加载时,我需要系统等待所有100个电话之前返回持续。我怎么会去吗?
I have a page that, using jQuery .ajax that is called 100 times (async: true), the problem is that, when they they are all being loaded, I need the system to wait for ALL 100 calls to return before continuing. How would I go about this?
在此先感谢! :)
更新:
这些电话由在for()循环(有他们的100:))
These calls are made in a for() loop (there's 100 of them :))
推荐答案
的很好的方式做,这是与 $。当
。您可以使用此如下:
The nice way to do this is with $.when
. You can use this as follows:
$.when(
$.ajax({/*settings*/}),
$.ajax({/*settings*/}),
$.ajax({/*settings*/}),
$.ajax({/*settings*/}),
).then(function() {
// when all AJAX requests are complete
});
另外,如果你把所有的AJAX调用数组,你可以使用<一个href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/apply"><$c$c>apply$c$c>:
$.when.apply($, ajaxReqs);
请注意,这至少需要的jQuery 1.5。
Note that this requires at least jQuery 1.5.
要在AJAX请求添加到一个数组,做这样的事情:
To add the AJAX requests to an array, do something like this:
var ajaxReqs = [];
for (var i = 0; i < 100; i++) {
ajaxReqs.push($.ajax({
/* AJAX settings */
});
}
$.when.apply($, ajaxReqs).then(function() {
// all requests are complete
});
这篇关于等待jQuery的Ajax响应(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!