JavaScript: Multiple clearTimeout in array

1.9k Views Asked by At

I have an array with timeout id's. What is the most elegant way to clear all of them at once? Is there a more efficient style than this?

waitHandler[1] = setTimeout('doSomethingA()', 2000);
waitHandler[2] = setTimeout('doSomethingB()', 2000);
...

for (var i=1; i < waitHandler.length; i++) {
    clearTimeout[i];
}
2

There are 2 best solutions below

0
On BEST ANSWER
waitHandler.forEach(clearTimeout);
1
On

I think what you mean to do is this:

for (var i=1; i < waitHandler.length; i++) {
    clearTimeout(waitHandler[i]);
}

Your old syntax wouldn't work.


And this is the only way to do it without plugins.