English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Window clearInterval() method

JavaScript Window Object

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);

Syntax:

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‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the clearInterval() method:

Method
clearInterval()11414

Parameter value

ParameterDescription
varsetInterval()The name of the timer returned by the method

Technical details

Return value:None

More examples

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‹/›

Related References

Window (Window) Reference:setInterval() method

Window (Window) Reference:setTimeout() method

Window (Window) Reference:clearTimeout() method

Window (Window) Reference:requestAnimationFrame() method

JavaScript Window Object