javascript - Prevent Multiple Instances for SetInterval
问题描述
What is the most recommended/best way to stop multiple instances of a setInterval function from being created (in javascript) and call it only once?
解决方案
setInterval
返回一个间隔 id,您可以使用它来检查它是否已经被实例化。例如:
let intervalId
const someFunction = () => {
intervalId = setInterval(someOtherFunction, 1000)
}
const someThirdFunction () => {
if (!intervalId) {
intervalId = setInterval(someOtherFunction, 1000)
}
}
推荐阅读
- c# - NUnit 3 驱动程序在执行反射代码时遇到错误 (NUnit.Engine.NUnitEngineException)
- r - 选择不同数据框中的缺失行
- php - 从 ms graph api 的 callrecords 中从外部 microsoft 团队用户获取信息
- c# - 从同步方法调用异步方法是 ASP .NET web api - 问题
- c# - 带有 Chrome 驱动程序的 C# selenium - 无法执行多个异步请求,puppeteer-sharp 也比 selenium 慢
- mysql - MySQL升级到8.0删除了我的旧数据?
- c# - MVC - 如何在没有括号 [0] 的情况下反序列化字典?
- c++ - VSCode C++ 删除一些成员变量时自动注释掉类头文件中的错误行
- python - 有没有更好的方法来遍历集合 (A, B, C) 选择 (a, b, c)
- ios - 如何知道图像属于哪个类?