16
rated 0 times
[
22]
[
6]
/ answers: 1 / hits: 26595
/ 9 Years ago, tue, june 30, 2015, 12:00:00
I'm trying to get user's city and country before moving on with my code. It seems as if javascript is not executed in the order I need.
$(document).ready(function() {
var country, city = '';
function geoData() {
$.getJSON('http://ipinfo.io/json?callback=?', function (data) {
console.log('step 1');
country = data.country;
city = data.city;
console.log('step 2');
});
};
geoData();
console.log('step 3');
/* rest of the code */
});
I want the code to be executed as:
step 1
step 2
step 3
However when I run the script I get:
step 3
step 1
step 2
Why is the code running in an asynchronous way? Any suggestions how I can fix it?
Thanks.
More From » jquery