Wait for two ajax-calls with jQuery.when
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
by HoffZ
HTML
<div id="output"></div>
JavaScript
// Info: http://net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/
function logger(txt) {
$('#output').append(txt + "<br>");
}
var url = '/echo/json';
var data = {'test': 'hello'};
var ajaxFunc1 = $.getJSON(url, data, function(json) {
logger('ajax1 done');
});
var ajaxFunc2 = $.getJSON(url, data, function(json) {
logger('ajax2 done');
});
$.when(ajaxFunc1, ajaxFunc2).done(function (data, textStatus, jqXHR) {
logger('ajax1 AND ajax2 done');
});
logger('script done');