I have been trying to learn Javascript/jQuery but I have hit a wall that I can't seem to break. This is my code:
function beginProblem() {
var myVar = getMyVar();
console.log(myVar);
}
function getMyVar(){
$.ajax({
url: "myTest.php",
success: function(result){
var myVar = result;
// return myVar; THIS DOESN'T WORK
}
});
return myVar; // This doesn't work either
}
beginProblem();
myVar is logging as undefined because the console.log runs before getMyVar is complete. I have read some other questions about this issue but I wasn't managed to understand the solution, can someone enlight me, what should I keep in mind when I need to obtain a variable from a ajax call and only then execute the rest of the function?
Sorry and thanks in advance!