如何在javascript中获取所有计时器? [英] How can I get all timers in javascript?
本文介绍了如何在javascript中获取所有计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在不同的类中使用 setTimeout()
函数创建了不同的计时器.我想知道是否有办法将所有超时放在一起?
I create different timer with setTimeout()
function in different class. I want to know if there is a way to get all timeouts together?
推荐答案
默认情况下没有,没有.您可以制作自己的模块,让您跟踪计时器,并为您提供列表.大致:
Not by default, no. You could make your own module that lets you keep track of the timers, and which gives you the list. Roughly:
// ES2015+ version
const activeTimers = [];
exports.setTimeout = (callback, interval, ...timerArgs) => {
const handle = setTimeout((...args) => {
const index = activeTimers.indexOf(handle);
if (index >= 0) {
activeTimers.splice(index, 1);
}
callback(...args);
}, interval, ...timerArgs);
activeTimers.push(handle);
};
exports.getActiveTimers = () => {
return activeTimers.slice();
};
...然后使用它的 setTimeout
而不是全局的.
...then use its setTimeout
instead of the global one.
这篇关于如何在javascript中获取所有计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文