Languages

Menu
Sites
Language
clearInterval(interval); How to find out Interval is cleared?

clearInterval(interval);

How to find out Interval is cleared?

(interval === null) doesn't work

(interval === 0) doesn't work

(!interval)doesn't work

Thanks!

Edited by: Anonymous on 03 May, 2019
View Selected Answer

Responses

8 Replies
Armaan-Ul- Islam

Try

( interval === undefined )

Thanks.

Thanks for your reple but interval === undefined doesn't work too :(

I've found a solution to set interval as "false" after clearing however it seems to be not the right way.

var interval = setInterval(function() {
                    }, 1000);

clearInterval(interval);

interval = false;

if (!interval){

}

Onur Şahin

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.

typeof interval === 'undefined' doesn't work to.

I want to clear it and in some cases to set again the interval if it is cleared. So I'm struggling to find out what kind of variable it after the method clearInterval()

Marco Buettner
var interval = 0;

interval = setInterval(function() {}, 1000);

clearInterval(interval);

console.log(typeof interval); // get the type

 

Mark as answer
Onur Şahin

Javascript is a pass by value language, meaning whatever you do with the parameter inside the function, it won't change the value of the variable inside the caller function. So interval variable stays with the same value after you call clearInterval.

 

Did you wrote interval  = clearInterval(interval); ? This should set interval to undefined thus typeof interval === 'undefined' should work.

Yep! It works great! Thanks!

No, I wrote "clearInterval(interval);"

Thanks, Marco Buettner. Type of setInterval variable is 'number'. In my case this number is '2' (both after clearing and setting an interval variable) maybe it is number of interval counters I have in my programm.

Unfortunately cheking by a number doesn't give opportunity to check it's either cleared or not.