First I have declared a global variable and set it to 0.
In the inner function I want to set the var to another value, but my output won't set in the (global)outer var, it will be set just as a local var. My other problem is the order in which I get the output.
Output:
third0
first0
second3
$(document).ready(function() {
state = 0;
$('#btnlogin').click(function() {
$.post("php/redirect.php", {
Username : $('#qi').attr('value'),
Password : $('#password').attr('value')
}, function(data) {
console.log('first'+state);
state = 3;
console.log('second'+state);
});
console.log('third'+state);
});
});
It happens because console.log('third'+state);
is outside $.post
(AJAX) call and calls faster than ajax response.
Seems that you misunderstood that AJAX is asynchronous.