jquery - Passing the counter of a for loop to queue()? -
this question has answer here:
i trying use value of loop counter inside queued function, example:
for(var i=0; i<5; i++) $(document.body).queue(function(){alert('i=' + i); $(this).dequeue();}).delay(1000);
then get:
i=0, i=5, i=5, i=5, i=5
i think because i
has changed while delay(1000)
..
can right values of i
, i.e:
i=0, i=1, i=2, i=3, i=4
using closure e.g:
for (var = 0; < 5; i++) $(document.body).queue(returnfromqueue(i)).delay(1000); function returnfromqueue(i) { return function () { alert('i=' + i); $(this).dequeue(); } }
Comments
Post a Comment