The clearInterval()
method clears a timer set with the setInterval() method.
clearInterval |
Yes | Yes | Yes | Yes | Yes |
clearInterval(id_of_setinterval)
Parameter | Description |
---|---|
id_of_setinterval | Required. The timer ID returned by the setInterval() method |
No return value.
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: