$.ajax is asynchronous. This means that it will return immediately, instead of waiting for the AJAX request to complete and receiving the result from the server. By the time the message arrived from the server, your ajaxIt function ajaxIt already completed its work.
What you should use here is a continuation style. Provide ajaxIt with the continuation: a function that explains what should be done after ajaxIt .
function ajaxIt(data, continuation) { data.ajax = true; $.post("ajax;php", data, function(msg) { console.log("[AjaxIt]: returned "+msg); continuation(msg); }); } ajaxIt({action:"logged-in"}, function(result) { if (result == "1") { console.log("Logged In"); loggedIn=true; initiate2(); } });
source share