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

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.


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

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;
}

You COULD override the setInterval method and add the capability to keep track of your intervals. Here is an untestet example to outline the idea. It will work on the current window only (if you have multiple, you could change this with the help of the prototype object) and this will only work if you override the functions BEFORE any functions that you care of keeping track about are registered:

var oldSetInterval = window.setInterval;
var oldClearInterval = window.clearInterval;
window.setInterval = function(func, time)
{
  var id = oldSetInterval(func, time);
  window.intervals.push(id);
  return id;
}
window.intervals = [];
window.clearInterval = function(id)
{
  for(int i = 0; i < window.setInterval.intervals; ++i)
    if (window.setInterval.intervals[i] == id)
    {
      window.setInterval.intervals.splice(i, 1);
    }
  oldClearInterval(id);
}
window.isIntervalRegistered(id)
{
  for(int i = 0; i < window.setInterval.intervals; ++i)
    if (window.setInterval.intervals[i] == func)
      return true;
  return false;
}



var i = 0;
var refreshLoop = setInterval(function(){
    i++;
}, 250);

if (isIntervalRegistered(refrshLoop)) alert('still registered');
else alert('not registered');
clearInterval(refreshLoop);
if (isIntervalRegistered(refrshLoop)) alert('still registered');
else alert('not registered');

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.