earpolicy
earpolicy

Reputation: 11

How to know when a collection of getJSON() requests are finished?

How to know when all of these requests are finished?

$.each(data.response.docs, function(i, item) {
    $.getJSON("jsp/informations.jsp?id=" + item.id, {}, function(data) {..});
});

Upvotes: 1

Views: 96

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337743

You could maintain a counter when each request completes, but that's a bit ugly and inelegant. Instead you could put the deferred objects returned from $.getJSON in to an array using map(). You can then apply than array to $.when. Something like this:

var requests = $.map(data.response.docs, function(i, item) {
    return $.getJSON('jsp/informations.jsp', { id: item.id }, function(data) {
        // ...
    });
}).get();

$.when.apply(requests).done(function() {
    // all requests complete...
});

Upvotes: 4

Related Questions