I have a $.getJSON request pulling data from an API. I need to pull data from that request to be used outside of it. Here's the code:
var weatherData = "http://api.openweathermap.org/data/2.5/weather?q=Location,us&appid=APIkey";
$.getJSON(weatherData,function(data){
var icon = data.weather[0].icon;
});
var skycons = new Skycons();
var conditions = [
"clear-day", "clear-night", "partly-cloudy-day",
"partly-cloudy-night", "cloudy", "rain", "sleet", "snow", "wind",
"fog"], i;
var icons = ["01d", "01n", "02d", "02n", "03d", "09d", "13d", "50d"];
if (icon == "03n" || icon == "04d" || icon == "04n")
icon = "03d";
if (icon == "09n" || icon == "10d" || icon == "10n" || icon == "11d" || icon == "11n")
icon = "09d";
if (icon == "13n")
icon = "13d";
if (icon == "50n")
icon = "50d";
for(i = conditions.length; i--; )
skycons.set(conditions[i], conditions[i]);
skycons.play();
The problem is, obviously, I need to be able to access "var icon = data.weather[0].icon;" outside of that $.getJSON request.
I understand jQuery is asynchronous. I've read every relevant question on here and I can't make sense of the answers. Please make it as simple as possible.