Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether clearInterval has been called?

Given this code:

bob = setInterval(function, 1000);
clearInterval(bob);

Is there now a way to know if that interval has been cleared?

Currently, I keep track of this myself, by unsetting 'bob', but I'm curious if my extra line of code is unnecessary:

clearInterval(bob);
bob = null;
if (!bob) itIsCleared();

Thanks!

like image 390
swajak Avatar asked Sep 24 '09 01:09

swajak


People also ask

How do you check if an interval is cleared?

When you call clearInterval, it gets the interval associated with that id and clears it. The id isn't changed at all. This will show you the interval's id (returned by setInterval earlier). If you know the interval's id is 1, you can just use clearInterval(1) to clear the interval.

Does clearInterval stop immediately?

I did make a fiddle to test it, and it turns out clearInterval stops all future execution, even those that have already be queued.

How do you know if an interval is running?

click(function(e) { clearInterval(interval); interval = null; }); And, you don't need to check to see if interval is undefined . You can just call clearInterval() on it and clearInterval() will protect against any invalid argument you pass it.

Can we call clearInterval inside setInterval?

Calling clearInterval() inside setInterval() has no effect But after calling clearInterval(), it will continue to execute.


3 Answers

The return value of setInterval is just a unique id you use to pass back to clearInterval. It's not a structured object with any additional information, nor does it get set to null when you call clearTimeout.

like image 101
Rich Avatar answered Oct 19 '22 21:10

Rich


bob only contains an id of the interval used to clear it. When you call clearInterval, it gets the interval associated with that id and clears it. The id isn't changed at all.

see here for demonstration

example:

<html>
<head>
<title>Javascript clearInterval</title>
</head>
<body onload="startInterval();">

<center>
    <div id="myTime"></div>

    <input type="button" value="start Interval" onclick="startInterval();" />

    <input type="button" value="stop Interval" onclick="stopInterval();" />
</center>

<script language="javascript">

var interval;

function startInterval()
{
    // setInterval of 1000 milliseconds i.e. 1 second
    // to recall the startTime() method again n again
    interval = setInterval("startTime();", 1000);
}

function startTime()
{
    // Date object to get current time
    var timeFormat = new Date();

    // set the current time into the HTML div object.
    document.getElementById('myTime').innerHTML = timeFormat.toLocaleTimeString();
}

function stopInterval()   //***********IMPORTANT FUNC******************
{
    // clearInterval to stop the setInterval event
    alert(interval);  
    clearInterval(1);

}

</script> 

</body>
</html>

This will show you the interval's id (returned by setInterval earlier). If you know the interval's id is 1, you can just use clearInterval(1) to clear the interval. So your way of using setting bob to null is a good way of doing it. Just be sure that !bob doesn't return true if the bob happens to be 0. :D

like image 7
Gordon Gustafson Avatar answered Oct 19 '22 21:10

Gordon Gustafson


So this is a very late answer but if you do something like this

this.myInterval = setInterval(....);
this.myInterval = clearInterval(this.myInterval);

It will force the variable myInterval to be undefined and thus you can check the clearing of the interval by doing if(!this.myInterval)

like image 6
Tikkes Avatar answered Oct 19 '22 21:10

Tikkes