This question seems super-primitive but I can't figure it out.
I have a code like this. I would expect the result be three alerts "0", "1" and "2". However, that's not what happens - I get three 2s.
for(var i=0; i<3; i++) {
var j=i;
setTimeout(function() {
alert(j);
},1000);
}
The code is at http://jsfiddle.net/8UMCA/.
How to "fix" the code so it alerts "0"-"1"-"2"?