与此问题几乎相同:请等待直到所有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.
我阅读了 api.jquery中的文档. com/jQuery.when/#jQuery-when-deferreds ,但该方法似乎不支持这种情况(将jqXHR的列表传递给$.when函数)
And I read the documents in api.jquery/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))传递给 $.when(),而不是数组,因此请使用 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请求列表完成?
发布评论