Reputation: 782
I have multiple ajax calls that I am making at one time. Once they are all done, I need to redirect the user to another page.
With the help of this answer, I created this:
function verifyAll() {
var ajaxCalls = [];
$.each($('.thing'), function(index, thing) {
ajaxCalls.push(verifyOne($(thing).data('id'));
});
$.when(ajaxCalls).done(function() {
console.log('Done verifying all');
});
}
function verifyOne(id) {
return $.ajax({
type: 'GET',
url: 'verify.html'
}).done(function(data) {
console.log('Done verifying one');
//do something
});
}
However, the $.when().done()
function is called BEFORE the .done()
is called on the individual ajax. Is there something I am doing wrong or is there another way to achieve my end goal?
Upvotes: 0
Views: 106
Reputation: 171679
$.when()
doesn't support arrays directly
Try
$.when.apply($, ajaxCalls).done(function() {
console.log('Done verifying all');
console.log(arguments);
});
Upvotes: 2