Having trouble figuring out how to access a variable inside a callback function from another function located outside of the callback...
To simplify the issue:
There is a function that retrieves JSON data from the web with some predefined arguments. Upon success, a callback function manipulates the data slightly. Another function responsible for updating a chart needs to receive the manipulated data from the callback and perform its own actions...
Here's my code snippet:
var mainData;
function getMainData(city, callback) {
this.city = city;
var url = "http://api.openweathermap.org/data/2.5/weather?q=appid=7ce3e1102e1902e0f878c2a640e95aed&london";
$.ajax ({
method: "GET",
type: "json",
cache: "false",
url: url,
success: callback
})
};
function callback(data) {
mainData = data;
var dArray = data.list.map(a=>a.dt)
var tempMinArray = data.list.map(a=>a.main.temp_min)
var tempMaxArray = data.list.map(a=>a.main.temp_max)
}
function changeChart(chartName, howToPassTempMinArrayHere?) {
chartName = chart.name.(....);
someDataFromMainData = chart.data.(....);
};
// on click of button (...) :
(
getMainData(callback); // why no action?? nothing??
changeChart (myChart, someDataFromMainData)
);