How To Save/clear Settimeout's Array Using Loop's Index?
I am calling a for loop multiple times. I would like to save a single setTimeout for each index. The idea is to use the loop's index as setTimeout's array index, but setTimeout ret
Solution 1:
You could use 2 dimensional array for this, first dimension will change along with the for loop index, whilst second dimension could remain in 0 to take control of the assigned timeout's IDs for each iteration.
For saving setTimeout within a index in a loop
var timeouts = []; // Two dimensional array
for (i = 0; i < 5; i++)
timeouts[i] = [];
for(var i=0; i < 5 ; i++) {
...
(function(delay, $element, savedtimeout){
savedtimeout[0] = setTimeout(function() {
countInView--;
}, delay, savedtimeout);
}(delay, $element, timeouts[i]));
...
}
For clear setTimeout within a index in a loop
if(timeouts[i][0] != null) {
//Removes the timeout from the queue
clearTimeout(timeouts[i][0]);
}
Post a Comment for "How To Save/clear Settimeout's Array Using Loop's Index?"