我可以看看计时器是否仍在运行吗? [英] Can I see if a timer is still running?
问题描述
这里简单的问题我似乎无法找到答案:一旦设置了 setTimeout
,有没有办法看看它是否仍然,好吧,设置?
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);
}
据我所知,当你 clearTimeout
,变量保持在其最后一个值。一个 console.log
我只是将计时器
显示为'12',无论是否设置了超时或清除。我是否也必须将变量置零,或者使用其他变量作为布尔说法,是的,我已设置此计时器?当然有一种方法可以检查超时是否仍在运行......对吗?我不需要知道剩下多长时间,只要它还在运行。
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.
推荐答案
无论如何都无法互动使用计时器除了启动或停止它。我通常在超时处理程序中清空timer变量,而不是使用标志来指示计时器未运行。有关计时器如何工作的 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.
这篇关于我可以看看计时器是否仍在运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!