function getData(jsonAddress) {
var data = new Array();
var xhr = $.getJSON(jsonAddress, function(meas) {
for (var i = 0; i < meas.length ; i++) {
var dt = datetimeSplitter(meas[i].valuedate);
data[i] = [Date.UTC(dt[0],dt[1]-1,dt[2],dt[3],dt[4],dt[5]),parseInt(meas[i].value,10)];
};
});
alert(data);
}
I would like to make the variable “data” available to the function getData so I can return it (in this case alert). I understood it is a problem of scope, and can be solved with closure. I understood also what is a closure, but definitely don’t know the syntax for this specific case.
It’s not a problem of scope, it’s a problem of asynchronous callbacks.
You need to pass in a callback function to this method, in order to get the response of your
getJSON()call.