I was testing closures in Fiddle.
The following piece of code prints all the i values from 0-9 but in random order.
for (var i = 0; i < 10; i++) {
(function (i) {
setTimeout(function () {
alert(i);
}, 2000);
})(i);
}
How do I overcome this problem, that is print the i values in proper order?
The question is not specific to any application/program, but just out of curiosity.
Update:
Only happens in Google Chrome.