I have this script:
for (var i = 1; i <= 2; i++) {
setTimeout(function() { alert(i) }, 100);
}
But 3
is alerted both times, instead of 1
then 2
.
Is there a way to pass i
, without writing the function as a string?
I have this script:
for (var i = 1; i <= 2; i++) {
setTimeout(function() { alert(i) }, 100);
}
But 3
is alerted both times, instead of 1
then 2
.
Is there a way to pass i
, without writing the function as a string?