I’m having issues with the following situation:
leeftijd:function(){
var testvar = "untouched";
var d = $('#gebdatumdd').val(),
m = $('#gebdatummm').val(),
y = $('#gebdatumjjjj').val();
x = "none!";
$.get("assets/incl/ageCheck.php", {y:y, m:m, d:d},
function(data){
console.log(data); // returns 'green';
console.log(testvar); // returns 'untouched';
testvar = data; // write data in testvar;
console.log(testvar); // returns 'green';
});
console.log('outside: ' + testvar); // returns 'untouched';
}
My ‘testvar’ just wont return the right value. Any ideas?
The ajax call is async. So that last
console.logis executed before the request has finished. You have to do all processing requiring the results of the request in the callback function.Another option would be using
$.ajaxwith theasync: falseoption. However, this may lock up the browser until the request has finished!