How To Access Index Variable In A Jquery Getjson Call ($.getjson) During A Loop?
I have the following code, which has been simplified for this question. Basically i have a loop, that, on each iteration, calls the jquery getJSON function, calling out to a API e
Solution 1:
Add a scoping function (an IIFE) that creates a new scope for your variable:
functionbuildCities()
{
for (var i = 0; i < 7; i++)
{
(function(index){
var jqxhr = $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=usa,phoenix&units=metric", function(response)
{
alert(index);
});
})(i);
}
}
Post a Comment for "How To Access Index Variable In A Jquery Getjson Call ($.getjson) During A Loop?"