i’ve made this function:
function fetchGeo(){
var geoInfo = new Array();
var counter = 0;
$.ajax({
url: 'www.URL.com/req.php',
dataType: 'json',
data: 'action=load',
success: function(data){
//console.log(data);
$.each(data, function(i,geo){
geoInfo[i] = new Array(geo.lat,geo.long,geo.time);
//console.log(geoInfo);
});
return geoInfo;
}
});
}
var geoInfo = fetchGeo();
Every time i execute this function i can’t get the data into a var somewhere else in the script.
I’ve already found this one, but that’s a fail for me too: Wait for .each() .getJSON request to finish before executing a callback
That’s because you are not returning anything from the
fetchGeofunction, you are returning it from the anonymous success handler, and that happens after thefetchGeofunction ends.Use a callback to handle the result: