English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
clearInterval()method cancels the use ofof setInterval() The repeated action created.
The ID value returned by setInterval() is used as the parameter for the clearInterval() method.
Note:To be able to use the clearInterval() method, a variable must be used when creating the interval method:
t = setInterval("javaScript function", milliseconds);
Then, you can stop the execution by calling the clearInterval() method:
clearInterval(t);
window.clearInterval(var)
var t = setInterval(startTimer, 1000); function startTimer() { var date = new Date(); var x = document.getElementById("result"); x.innerHTML = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); } function stopTimer() { clearInterval(t); }Test and See‹/›
The numbers in the table specify the first browser version that fully supports the clearInterval() method:
Method | |||||
clearInterval() | 1 | 1 | 4 | 1 | 4 |
Parameter | Description |
---|---|
var | setInterval()The name of the timer returned by the method |
Return value: | None |
---|
Every200 milliseconds to switch between two background colors until it is stopped by clearInterval():
var t = setInterval(setColor, 200); function setColor() { var x = document.body; x.style.backgroundColor = (x.style.backgroundColor == "coral") ? "lightgreen" : "coral"; } function stopColor() { clearInterval(t); }Test and See‹/›
Create a dynamic progress bar using setInterval() and clearInterval():
var i = 0; var bar = document.getElementById("progress-bar); var t; function start() { t = setInterval(progress, 60); } function progress() { if(i < 100) { i++; bar.style.width = i + “%”; bar.innerHTML = i + “ %”; } else { clearInterval(t); } } function stop() { clearInterval(t); }Test and See‹/›
Window (Window) Reference:setInterval() method
Window (Window) Reference:setTimeout() method
Window (Window) Reference:clearTimeout() method
Window (Window) Reference:requestAnimationFrame() method