Javascript Reference - Window clearInterval() Method








The clearInterval() method clears a timer set with the setInterval() method.

Browser Support

clearInterval Yes Yes Yes Yes Yes

Syntax

clearInterval(id_of_setinterval)

Parameter Values

Parameter Description
id_of_setinterval Required. The timer ID returned by the setInterval() method




Return Value

No return value.

Example

The following code shows how to display the current time.


<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<button onclick="myStopFunction()">Stop time</button>
<script>
var myVar = setInterval(function(){myTimer()}, 1000);
<!--from ww w. j a va2  s .  com-->
function myTimer() {
    var d = new Date();
    var t = d.toLocaleTimeString();
    document.getElementById("demo").innerHTML = t;
}

function myStopFunction() {
    clearInterval(myVar);
}
</script>

</body>
</html>

The code above is rendered as follows: