I have a very simple test page that tests jquery (1.4.2) queue and delay.
for (var i = 1; i <= 5; i++) {
$('#test')
//.delay(50)
.queue(function(next) {
console.log(i);
next();
});
}
Now when I run this code in FF with firebug, I get what I expected, 1 ~ 5.
However, if I un-comment delay, I got 6 five times instead?
Can someone please help me clarify this?
The
iis a single variable stored one time and shared by all iterations of the loop. Without the.delay()you’re using the value ofiright then, so it’s what you expect. With the.delay()however, you’re using what the value is later…and later it’s what it ended up as at the end of the loop, 6.