Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Post a Comment for "How To Know When A Collection Of Getjson() Requests Are Finished?"