I'm trying to call an ajax function for every selected checkbox on a page. Once all of those ajax calls have completed, I want to do something else. In the example below, the 'all calls completed' text is written to the console before any of the 'single call completed' text. How can I make this wait until all ajax calls are finished? Thanks!!
function ajax_function() {
return $.ajax({
url: "submit.php",
type: "POST",
dataType: "json",
data: form_data,
cache: false
})
}
var deferreds = $('input:checkbox:checked.checkprint').map(function(i, elem) {
$.when(ajax_function()).then(function(data) {
console.log('single call completed');
return data;
});
});
$.when.apply($, deferreds.get()).done(function() {
console.log('all calls completed');
});