H.C H.C - 1 year ago 38
Ajax Question

Multiple Ajax Requests (with one callback)

I'm sending multiple ajax requests and want to get a callback if all the requests are successful. I've found

$.when($.ajax(), [...]).then(function(results){},[...]);
but that only works when you know in advance how many you're going to do. In my case, it varies depending on user input.

I tried the following but I'm not sure where or how
fits in:

$('#piecesTable tr').not(':first').each(function(){

// ...some prep...

// ...args here...
).then(function() {
// All requests are done

How do I use the results of all those separate
calls with
? Or do I handle this some other way?

Answer Source

I think the general structure of what you're looking for is something like this:

var requests = [];

// Populate requests array with ajax requests.
    // ...

// Add as many requests as you want to the array.

$.when.apply($, requests).done(function() {
    var args = $.prototype.slice.call(arguments);

    // args should contain the results of your ajax requests.

    // Do whatever with the results.