gpt4 book ai didi

javascript - 在 setInterval() 之前调用 clearInterval() 可以吗?

转载 作者:数据小太阳 更新时间:2023-10-29 04:34:41 24 4
gpt4 key购买 nike

我有一段代码:

if (someCondition) {
// clear globTimer first??
globTimer = setInterval(function() {
someBlinkingCode;
}, 1000);
} else {
clearInterval(globTimer);
}

但这部分代码可以多次调用,其中 someCondition 将为真。这意味着将创建多个间隔,而不是所有间隔都将被销毁。一段时间后,闪烁频率超过 1 秒,因此我添加了 clearInterval(globTimer); 而不是注释。此更改解决了我的问题,但此解决方案可以吗?是否可以为同一个变量多次调用 clearInterval() 或为 undefined 调用它?

最佳答案

是的,可以使用“无效”标识符调用 clearInterval(和 clearTimeout)。 The specification说:

The clearInterval() method must clear the entry identified as handle from the list of active intervals of the WindowTimers object on which the method was invoked, where handle is the argument passed to the method, if any. (If handle does not identify an entry in the list of active intervals of the WindowTimers object on which the method was invoked, the method does nothing.)

(强调我的)

关于javascript - 在 setInterval() 之前调用 clearInterval() 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com