admin管理员组文章数量:1415467
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];
}
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];
}
Share
Improve this question
asked Nov 18, 2014 at 9:56
RobbitRobbit
1,5872 gold badges9 silver badges10 bronze badges
4
-
Don't write code in strings.
var t = 2000; var ids = [setTimeout(doSomethingA, t), ...];
– 1983 Commented Nov 18, 2014 at 10:33 - Okay, but what if I need to consign params? – Robbit Commented Nov 18, 2014 at 11:42
-
Then use anonymous functions:
setTimeout(function(){doSomethingA(param)})
– Scimonster Commented Nov 18, 2014 at 11:44 - Ah, is this more performant or just a better coding style? – Robbit Commented Nov 18, 2014 at 12:58
2 Answers
Reset to default 8waitHandler.forEach(clearTimeout);
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.
本文标签: JavaScript Multiple clearTimeout in arrayStack Overflow
版权声明:本文标题:JavaScript: Multiple clearTimeout in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232087a2648869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论