等到jquery ajax请求列表完成? [英] Wait until a list of jquery ajax request are done?
问题描述
与此问题几乎相同:请等待直到所有jQuery Ajax请求完成了吗?
但是我的问题很糟糕:
我正在处理许多ajax请求,并将它们保存在一个数组中:
That I'm doing many ajax requests, and I save them in an array:
var events = [];
for(var i = 0; i < 10; ++i) {
events.push($.ajax({...}));
}
$.when(events).done(function() {
// do something when all requests are done.
});
正如相关问题所表明的那样,$ .when方法按顺序按一些jqXHR
对象,但是在这里我要其中的一个列表.
As the related question tells, the $.when method takes some jqXHR
object in order, but here I want a list of them taken.
我阅读了 http://api.jquery中的文档. com/jQuery.when/#jQuery-when-deferreds ,但该方法似乎不支持这种情况(将jqXHR
的列表传递给$.when
函数)
And I read the documents in http://api.jquery.com/jQuery.when/#jQuery-when-deferreds, but the method seemed not supporting this case (passing a list of jqXHR
to $.when
function)
怎么可能?请帮助.
推荐答案
,您需要将Promise作为参数列表(如$.when(promise1, promise2, promise3)
)传递给 Function.apply().
you need pass the promises as an argument list(like $.when(promise1, promise2, promise3)
) to $.when(), not as an array so use Function.apply().
$.when.apply($, events).then(function() {
// do something when all requests are done.
});
这篇关于等到jquery ajax请求列表完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!