1

It is almost the same with this question: Wait until all jQuery Ajax requests are done?

But my question is imporve:

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.
});

As the related question tells, the $.when method takes some jqXHR object in order, but here I want a list of them taken.

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)

How could it be? Plz help.

Community
  • 1
  • 1
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189

1 Answers1

6

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.
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531