I have a function that calls other functions which have AJAX requests. I want all of the AJAX calls to complete before calling the last function. I initialize an array and need all of the AJAX calls to have successfully completed before going to the last step. When I run the code in the browser the console is showing the last method being called before the first AJAX call is completed. I've tried using .done
however I'm still getting the same result.
function Search() {
var userIds = [];
var certName = $('#certificationName').val();
var skillNumberOne = $('#skillName1').val();
var skillNumberTwo = $('#skillName2').val();
var skillNumberThree = $('#skillName3').val();
var locale = $('#location').val();
CertMatchResults(certName, userIds);
SkillMatchResults(skillNumberOne, userIds);
SkillMatchResults(skillNumberTwo, userIds);
SkillMatchResults(skillNumberThree, userIds);
LocationMatchResults(locale, userIds);
console.log(userIds);
DisplayMatches(userIds);
}
function CertMatchResults(certName, userIds) {
if (certName == '') return;
$.getJSON('json_data.php', { method: 'matchCerts', certName: certName }, function(data) {
$.each(data, function(key, value) {
console.log("Cert Match >>> " + value.user_id);
userIds.push(value.user_id);
});
}).done(function() {
console.log("Cert match completed.");
});
}
function SkillMatchResults(skillName, userIds) {
if (skillName == '') return;
$.getJSON('json_data.php', { method: 'matchSkill', skillName: skillName }, function(data) {
$.each(data, function(key, value) {
console.log("Skill Match >>> " + value.user_id);
userIds.push(value.user_id);
});
});
}
... other calls below ... I can add them if needed
DisplayMatches
needs all of the AJAX calls in each of the functions before it to complete.