You can use the promise returned by $.ajax to set up a flexible callback queue as follows:
var requests = []; //Array containing all the ajax calls for (var i = 0; i < 9; i++) { requests.push( $.ajax({ url: '/echo/html', //this is because of jsfiddle.net type: 'post', //this one too success: function() { //whatever } })); } $.when.apply($, requests).then(function() { //.apply is needed as we want to pass an Array //called when all requests are done }).fail(function(){ //this will be triggered when one of the requests fails //error handling can go here });
See this working fiddle and read about .when() and .then
In your case, this will end:
var numSuccessful = 0; var requests = $.makeArray($('.mySelector').map(function(){ return $.ajax({ url: '/myCfc.cfc?method=doSomething&id=' + this.id, type: 'GET' }).done(function(){ numSuccessful++; }); })); $.when.apply($, requests).then(function() { alert(numSuccessful + ' successful'); });โ
source share