Reputation: 730
I have to clear multiple setTimeout functions
$('#stop').click(function(){
clearTimeout(anim1);
clearTimeout(anim2);
clearTimeout(anim3);
clearTimeout(anim4);
clearTimeout(anim5);
clearTimeout(anim6);
clearTimeout(anim7);
clearTimeout(anim8);
clearTimeout(anim9);
clearTimeout(anim10);
});
Is there any way to short this code something like clearTimeout(anim1,anim2,anim3...);
I have already tried this with coma separation but it doesn't work that way.
Upvotes: 2
Views: 247
Reputation: 4685
You put them in an array and iterate over them
var timers = [anim1, anim2, anim3, anim4] //can also be added when created
for (var t=0;t<timers.length;t++) {
clearTimeout(timers[t]);
}
In general when you need to perform the same operation on many or an unknown amount of objects you should put them in an array or structure them in an object where you can adress "all" items programmaticly. Heres a comphrehensive guide to indexed collections
Upvotes: 2