I need to wait until the three ajax calls end before another function starts. I tried using jquery deferred promise https://api.jquery.com/deferred.promise/ , but my jobs return before loading deferred data. I need to get these three ajax calls to complete before I run the function. I am not attached to using deferred, it just seemed like a logical way.
I use datatables and need ajax calls to complete before the data is loaded into them.
"ajax":{
"url": API_ROOT + "jobs?available=true&max_results=500",
"dataSrc": function(data){
function all_loaded(){
var dfd = $.Deferred();
var mats_loaded, fins_loaded, pros_loaded;
setTimeout(function(){
$.ajax({
url: API_ROOT + "finishes?max_results=500",
dataType: 'json',
error: function(){
console.log("Error getting finishes");
},
success: function(data){
finishes = data._items;
fins_loaded = true;
check_all_loaded();
}
});
},2000);
$.ajax({
url: API_ROOT + "processes?max_results=500",
dataType: 'json',
error: function(){
console.error("Error getting processes");
},
success: function(data){
processes = data._items;
pros_loaded = true;
check_all_loaded();
}
});
$.ajax({
url: API_ROOT + "materials?max_results=500",
dataType: 'json',
error: function(){
console.log("Error getting materials");
},
success: function(data){
materials = data._items;
mats_loaded = true;
check_all_loaded();
}
});
check_all_loaded = function(){
if (mats_loaded && fins_loaded && pros_loaded){
dfd.resolve("Loaded");
}
}
return dfd.promise();
}
$.when( all_loaded()).then(function(){
var jobs = data._items;
return jobs;
});
}
}
, , , .when , Ajax . , .when js , .
, , Ajax. .