Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  97] [ 1]  / answers: 1 / hits: 83325  / 14 Years ago, tue, april 20, 2010, 12:00:00

For instance, I am setting an interval like



timer = setInterval(fncName, 1000);


and if i go and do



clearInterval(timer);


it does clear the interval but is there a way to check that it cleared the interval? I've tried getting the value of it while it has an interval and when it doesn't but they both just seem to be numbers.


More From » javascript

 Answers
21

There is no direct way to do what you are looking for. Instead, you could set timer to false every time you call clearInterval:



// Start timer
var timer = setInterval(fncName, 1000);

// End timer
clearInterval(timer);
timer = false;


Now, timer will either be false or have a value at a given time, so you can simply check with



if (timer)
...


If you want to encapsulate this in a class:



function Interval(fn, time) {
var timer = false;
this.start = function () {
if (!this.isRunning())
timer = setInterval(fn, time);
};
this.stop = function () {
clearInterval(timer);
timer = false;
};
this.isRunning = function () {
return timer !== false;
};
}

var i = new Interval(fncName, 1000);
i.start();

if (i.isRunning())
// ...

i.stop();

[#97012] Sunday, April 18, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daryldeontaeh

Total Points: 46
Total Questions: 97
Total Answers: 105

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;