Is there a way to check if a var is using setInterval()?

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();

Well you can do

var interval = setInterval(function() {}, 1000);
interval = clearInterval(interval);
if (typeof interval === 'undefined'){
...
}

but what are you actually trying to do? clearInterval function is an always success function and it will always return undefined even if you call it with a NaN value, no error checking in there.


The return values from setTimeout and setInterval are completely opaque values. You can't derive any meaning from them; the only use for them is to pass back to clearTimeout and clearInterval.

There is no function to test whether a value corresponds to an active timeout/interval, sorry! If you wanted a timer whose status you could check, you'd have to create your own wrapper functions that remembered what the set/clear state was.


I did this like below, My problem was solved. you should set the value like "false", when you clearTimeout the timer.

var timeer=false;
----
----
if(timeer==false)
{
  starttimer();  
}
-----
-----
function starttimer()
{
  timeer_main=setInterval(activefunction, 1000);
  timeer=true;
}

function pausetimer()
{
  clearTimeout(timeer_main);
  timeer=false;
}

Tags:

Javascript