我能看看计时器是否还在运行吗?

这里有一个简单的问题,我似乎找不到答案: 一旦 setTimeout设置好了,有没有办法看看它是否仍然,嗯,设置好了?

if (!Timer)
{
Timer = setTimeout(DoThis,60000);
}

据我所知,当你使用 clearTimeout时,变量保持在最后一个值。我刚刚查看的 console.log显示 Timer为“12”,不管超时是设置了还是清除了。我是否也必须使变量为空,或者使用一些其他的变量作为布尔表示,是的,我已经设置了这个计时器?当然有一种方法可以检查超时是否仍在运行... 对吗?我不需要知道还剩多少时间,只需要知道它是否还在运行。

116215 次浏览

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.

What I do is:

var timer = null;


if (timer != null) {
window.clearTimeout(timer);
timer = null;
}
else {
timer = window.setTimeout(yourFunction, 0);
}

Set another variable Timer_Started = true with your timer. And also change the variable to false when the timer function is called:

// 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.");
}


}

I know this is a necroposting but i think still people are looking for this.

This is what i use: 3 variables:

  1. t for milliseconds since.. in Date Object for next target
  2. timerSys for the actual interval
  3. seconds threshold for milliseconds has been set

next 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 set global var timerSys to false;

var t, timerSys, seconds;


function timer(s) {
if (s && typeof s === "number") {
if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
timerSys = setInterval(function() {
sys();
}, s);
t = new Date().setMilliseconds(s);
seconds = s;
}
} else {
clearInterval(timerSys);
timerSys = false;
}
return ((!timerSys) ? "0" : t)
}


function sys() {
t = new Date().setMilliseconds(seconds);


}

Example I

Now you can add a line to sys function:

function sys() {
t = new Date().setMilliseconds(seconds);
console.log("Next execution: " + new Date(t));
//this is also the place where you put functions & code needed to happen when interval is triggerd


}

And execute :

  timer(5000);

Every 5 seconds in console:

  //output:: Next execution: Sun May 08 2016 11:01:05 GMT+0200 (Romance (zomertijd))

Example II

function sys() {
t = new Date().setMilliseconds(seconds);
console.log("Next execution: " + seconds/1000 + " seconds");


}


$(function() {
timer(5000);
});

Every 5 seconds in console:

      //output:: Next execution: 5 seconds

Example III

var t, timerSys, seconds;


function timer(s) {
if (s && typeof s === "number") {
if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
timerSys = setInterval(function() {
sys();
}, s);
t = new Date().setMilliseconds(s);
seconds = s;
}
} else {
clearInterval(timerSys);
timerSys = false;
}
return ((!timerSys) ? "0" : t)
}


function sys() {
t = new Date().setMilliseconds(seconds);
console.log("Next execution: " + seconds / 1000 + " seconds");


}


$(function() {
timer(5000);


$("button").on("click", function() {
$("span").text(t - new Date());
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Freebeer</button>
<span></span>

Note this way you can go below 0

I usually nullify the timer:

var alarm = setTimeout(wakeUpOneHourLater, 3600000);
function wakeUpOneHourLater() {
alarm = null;    //stop alarm after sleeping for exactly one hour
}
//...
if (!alarm) {
console.log('Oops, waked up too early...Zzz...');
}
else {
console.log('Slept for at least one hour!');
}

There is no need to check for an existing timer, just execute clearTimeout before starting the timer.

var timer;
//..
var startTimer = function() {
clearTimeout(timer);
timer = setTimeout(DoThis, 6000);
}

This will clear any timer before starting a new instance.