'Javascript Function Call have different behaviour compared to run by console in web browser
i have this code:
function foo(){
var x = $.getJSON('./someFileName.txt',function(data){});
var y = x.responseText;
console.log(y);
}
when i called it via browser console (firefox), it give me undefined for the console log result.
But, when i just copy, paste and, run the foo() function body line by line in my browser console, it gives me a result which is a string.
Can anyone enlighten me why this behaviour happens? I suppose it has something to do with the jquery call but i am not sure.
Thanks!
Solution 1:[1]
You need to place the second and third lines of your function inside the callback like this:
function foo(){
$.getJSON('./someFileName.txt',function(data){
var y = data.responseText;
console.log(y);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mifo |
