我可以查看计时器是否仍在运行吗?
这里有一个简单的问题,我似乎找不到答案:设置 setTimeout 后,有什么方法可以查看它是否仍然设置?
if (!Timer)
{
Timer = setTimeout(DoThis,60000);
}
据我所知,当您clearTimeout
时,变量将保持其最后的值。我刚刚查看的 console.log
显示 Timer
为“12”,无论超时是否已设置或清除。我是否也必须将变量清空,或者使用其他变量作为布尔值,说“是的,我已经设置了这个计时器”?当然有一种方法可以检查超时是否仍在运行......对吧?我不需要知道还剩多长时间,只要知道它是否仍在运行即可。
Simple question here that I can't seem to find an answer for: Once a setTimeout
is set, is there any way to see if it's still, well, set?
if (!Timer)
{
Timer = setTimeout(DoThis,60000);
}
From what I can tell, when you clearTimeout
, the variable remains at its last value. A console.log
I just looked at shows Timer
as being '12', no matter if the timeout has been set or cleared. Do I have to null out the variable as well, or use some other variable as a boolean saying, yes, I have set this timer? Surely there's a way to just check to see if the timeout is still running... right? I don't need to know how long is left, just if it's still running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我所做的是:
What I do is:
除了启动或停止计时器之外,无法与计时器进行交互。我通常将超时处理程序中的计时器变量设置为空,而不是使用标志来指示计时器未运行。 W3Schools 上有关于计时器如何工作的很好的描述。在他们的示例中,他们使用标志变量。
您看到的值是当前的句柄计时器,当您清除(停止)它时使用它。
There isn't anyway to interact with the timer except to start it or stop it. I typically null the timer variable in the timeout handler rather than use a flag to indicate that the timer isn't running. There's a nice description on W3Schools about how the timer works. In their example they use a flag variable.
The value you are seeing is a handle to the current timer, which is used when you clear (stop) it.
无需检查现有计时器,只需在启动计时器之前执行
clearTimeout
即可。这将在启动新实例之前清除所有计时器。
There is no need to check for an existing timer, just execute
clearTimeout
before starting the timer.This will clear any timer before starting a new instance.
使用计时器设置另一个变量
Timer_Started = true
。并且在调用计时器函数时将变量更改为 false:Set another variable
Timer_Started = true
with your timer. And also change the variable tofalse
when the timer function is called:我知道这是一个死亡帖子,但我认为仍然有人在寻找它。
这就是我使用的:
3 个变量:
t
表示毫秒,因为在下一个目标的日期对象中timerSys
表示实际间隔秒
毫秒阈值已在下一个 i 中 设置有一个带有 1 个变量的
函数计时器
,该函数检查变量是否真正,如果是,则检查计时器是否已经在运行,如果是这种情况,则填充 global vars,如果不是true,falsely,则清除间隔并将global var timeSys
设置为false;示例 I
现在您可以向 sys 函数添加一行:
并执行:
控制台中每 5 秒:
示例 II
控制台中每 5 秒:
示例三
请注意,这样您可以低于 0
I know this is a necroposting but i think still people are looking for this.
This is what i use:
3 variables:
t
for milliseconds since.. in Date Object for next targettimerSys
for the actual intervalseconds
threshold for milliseconds has been setnext i have a
function timer
with 1 variable the function checks if variable is truly, if so he check if timer is already running and if this is the case than fills the global vars , if not truly, falsely, clears the interval and setglobal var timerSys
to false;Example I
Now you can add a line to sys function:
And execute :
Every 5 seconds in console:
Example II
Every 5 seconds in console:
Example III
Note this way you can go below 0
我通常取消计时器:
I usually nullify the timer:
我创建了一个从timer.ts导出的计时器服务,如下所示 -
我在创建或清除超时时使用它 -
添加/删除计时器服务会产生一些开销,但它会显示在本地存储中以供快速检查。我很少使用这项服务,因为我的应用程序中没有设置太多超时。
在indexedDb中显示如下 -
I created a timer service as below exported from timer.ts -
I use this whilst creating or clearing a timeout -
A bit of overhead to add/remove to timer service but it shows up in the local storage for a quick check. I have very few use for this service since I don't have much timeouts set in my app.
Showed like this in the indexedDb -