I'm trying to use a for
loop to cycle through an array of URLs and use them for ajax calls. Outside of the ajax call, i
changes value correctly, but when I try and access it from inside the call it always returns 2
. It loops correctly but with the same value instead of cycling through 0, 1
etc.
var i = 0;
for(i = 0; i <= 1; ++i) {
console.log("Value outside of call = " + i);
$.ajax({
url : urls[i],
dataType : 'jsonp',
timeout : 3000,
count : 0,
success : function(data) {
console.log("Value inside of call = " + i);
shotInfo[i] = data;
},
error : function() {
}
})
}
I've tried using a while
loop too, but it has the same effect.