I must be making a basic javascript error. My script is failing to pass a variable, and I can't figure out why. Here's the code, with comments.
var earth;
function loadWorld(world) {
// loads the world from a Json file using php
$.ajax("loadfile.php", {
dataType: "json",
async: false
}).done(function (result) {
console.log(result);
//Here consul.log provides the expected results, confirming that the file has been loaded into the result.
world = result;
console.log(world);
//Here too, properly transferred into world.
});
};
function button1() {
loadWorld(earth);
console.log(earth);
//But here console.log tells me earth is undefined!
showData(earth);
}
I've tried using earth as a global variable and simply assigning it in function loadworld(), like this:
earth = result;
I've also tried using a return function. But once I leave loadworld, earth is always undefined. Any ideas why?