And*_*rew 63 javascript settimeout
这里简单的问题是我似乎无法找到答案:一旦setTimeout确定了,有没有办法看看它是否仍然,好吧,设置?
if (!Timer)
{
Timer = setTimeout(DoThis,60000);
}
Run Code Online (Sandbox Code Playgroud)
从我所知,当你clearTimeout,变量保持在它的最后一个值.一个console.log我只是看着显示Timer为"12",不管是否设置了超时或清除.我是否也必须将变量置零,或者使用其他变量作为布尔说法,是的,我已设置此计时器?当然有一种方法可以检查超时是否仍在运行......对吗?我不需要知道剩下多长时间,只是它还在运行.
Ben*_*uer 53
我所做的是:
var timer = null;
if (timer != null) {
window.clearTimeout(timer);
timer = null;
}
else {
timer = window.setTimeout(yourFunction, 0);
}
Run Code Online (Sandbox Code Playgroud)
无需检查现有的计时器,只需clearTimeout在启动计时器之前执行即可。
var timer;
//..
var startTimer = function() {
clearTimeout(timer);
timer = setTimeout(DoThis, 6000);
}
Run Code Online (Sandbox Code Playgroud)
这将在启动新实例之前清除所有计时器。
小智 5
Timer_Started = true用你的计时器设置另一个变量。并且还将变量更改false为调用计时器函数时:
// set 'Timer_Started' when you setTimeout
var Timer_Started = true;
var Timer = setTimeout(DoThis,60000);
function DoThis(){
// function DoThis actions
//note that timer is done.
Timer_Started = false;
}
function Check_If_My_Timer_Is_Done(){
if(Timer_Started){
alert("The timer must still be running.");
}else{
alert("The timer is DONE.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75390 次 |
| 最近记录: |