gpt4 book ai didi

javascript - 在clearInterval(id_of_setinterval)之后是否需要将id_of_setinterval变量设置为null?

转载 作者:行者123 更新时间:2023-11-28 05:17:18 25 4
gpt4 key购买 nike

var id_of_setinterval = setInterval(function(){}, 2000)

if(id_of_setinterval) {
clearInterval(id_of_setinterval);
id_of_setinterval = null; //Does it necessary? Why?
}

我真的不知道为什么要写这段代码。

在我看来,将 id_of_setinterval 设置为 null不必要的。

最佳答案

这取决于。

如果您使用多个计时器并启动和结束某个计时器,那么最好将其设置为零,因为它只保存计时器的 ID,从 setInterval :

The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to Window.clearInterval() to cancel the timeout.

并且根据clearInterval

It's worth noting that the pool of IDs used by setInterval() and setTimeout() are shared, which means you can technically use clearInterval() and clearTimeout() interchangeably. However, for clarity, you should avoid doing so.

如果只使用一个定时器和一个变量,则无需将 id 设置为零。

关于javascript - 在clearInterval(id_of_setinterval)之后是否需要将id_of_setinterval变量设置为null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40869259/

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