Friday, 30 June 2017

javascript - setTimeout only sees last value in array?








I have an array of ids which I loop over and want to use in a function called by setTimeout, however when "func" below is executed it only seems to see the last id stored in the array.
I have been trying to uses closures to fix the issue but have had no success.



  // loop over array an call setTimeout for loading an image
for (var i = 0; i < idlist.length; i++) {
// variable i want use in function
var lookup = idlist[i];


var func = function() {
alert(lookup); // this is always the last value in the "idlist" array
};

setTimeout(func, 500);
}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...