gpt4 book ai didi

javascript - 我应该如何声明一个具有 setInterval 值的变量?

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

我正在查看一个问题,有人用下面的代码回答了它,并在评论中有人说

I think instead of assigning undefined we should assign 0 to this.tm. As it's time interval and it should not be undefined.

constructor(props) {
super(props);

this.tm; // With what value should this be declared?
}

timerInterval = () => {
this.tm = setInterval(() => {
...
}, 1000);
}

render(){
return(...)
}

所以这让我想知道,它应该声明什么值?从 undefined 更改为 0 有什么区别吗?

来自文档或类似内容的引用文献可以很好地解释它。我没找到。

最佳答案

所以,结合所有评论

  • 我不需要在构造函数中声明它,我可以正常使用 this.tm 。如果我声明它,这只是一种让代码更清晰的好习惯。
  • This answer对于为什么最好将其设置为 0 有正确的解释,但仅限于浏览器。如果是node环境,最好设置为null。

    On browsers, the handle is guaranteed to be a number that isn't equal to 0; therefore, 0 makes a handy flag value for "no timer set". (Other platforms may return other values; NodeJS's timer functions return an object, for instance.)

关于javascript - 我应该如何声明一个具有 setInterval 值的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524306/

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