0

I want to store a value from a JSON object in a global variable to use later in my code. Apparently this does not work even though it should make the country variable global.

$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
    window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);
Axel K
  • 283
  • 1
  • 5
  • 11

1 Answers1

1

The getJSON call is asynchronous. Put that console.log call inside the callback and it will not be undefined.

musicnothing
  • 3,977
  • 24
  • 43