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

Javascript

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Solution 6 - Javascript

The solution to this problem: Create a global counter that is incremented within your code performed by setInterval. Then before you recall setInterval, test if the counter is STILL incrementing. If so, your setInterval is still active. If not, you're good to go.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchadleyView Question on Stackoverflow
Solution 1 - JavascriptCasey ChuView Answer on Stackoverflow
Solution 2 - JavascriptbobinceView Answer on Stackoverflow
Solution 3 - Javascriptuser3851829View Answer on Stackoverflow
Solution 4 - JavascriptVibhor DubeView Answer on Stackoverflow
Solution 5 - JavascriptyankeeView Answer on Stackoverflow
Solution 6 - JavascriptThe1stEsonianView Answer on Stackoverflow