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?

10 Answers
10

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *