My code is:
var myvarible = "0";
wialon.util.Gis.getLocations(coordsc,function(code,responceval) {
myvarible = responceval;
})
console.log(myvarible); // output is 0
How i can fix my problem? I think while it returns response code console.log runs first. please help with my problem.
Assuming that this relies on the geolocation functionality of the browser, you can’t “fix” this problem. It’s in the nature of asynchronous coding.
In your function
wialon.util.Gis.getLocations()you pass in a callback. So after the (asynchronous) execution of this call, your callback will be executed.Meanwhile the rest of your code is executed, which leads to the
console.log(myvarible);resulting in a 0.Change your coding style accordingly and put all code dependent on
myvariblein the callback itself or in a function, that is called inside the callback. That way this code is only executed aftermyvaribleis set.So either:
or