This is the very frequently repeated “how do I use a loop variable in a closure” problem.
The canonical solution is to call a function which returns a function that’s bound to the current value of the loop variable:
var strings = [ "hello", "world" ];
var delay = 1000;
for(var i=0;i<strings.length;i++) {
setTimeout(
(function(s) {
return function() {
alert(s);
}
})(strings[i]), delay);
delay += 1000;
}
The outer definition function(s) { ... }
creates a new scope where s
is bound to the current value of the supplied parameter – i.e. strings[i]
– where it’s available to the inner scope.