The clearInterval() method in JavaScript clears the timer which has been set by a setInterval() method.

var interval = setInterval(function(){
  //Code that will run every 1000 milliseconds
  if( //some condition ){
    clearInterval(interval);
  }
}, 1000);

In the code above, the setInterval() method will run the function every 1000 milliseconds(1 second). We can change that parameter to be however often we want to execute the function call.

You can see there is an if conditional statement in the code above. Usually once a certain condition is met, we then will want to end the setInterval() method. We can do this by setting a variable to the setInterval method, var interval = setInterval(), as seen above, and then clearing it by using the clearInterval() method in JavaScript, clearInterval(interval);.

We find the best way to learn how to use the clearInterval() method is to see more examples of it. So let’s take a look at one below.


To see how the clearInterval() method in JavaScript works, let’s create a simple example. Here is code that will run a simple countdown timer.

Let’s start with the simple HTML setup:

Start Countdown

Below will be the JavaScript code to make this countdown timer work.

var counterStartTime = 6;
var interval = setInterval(function(){
  counterStartTime--;
  document.getElementById("countdown-text").innerHTML = "" + counterStartTime + "";
  if( counterStartTime == 0 ){
    clearInterval(interval);
    alert("5 seconds have passed.");
  }
}, 1000);

The basic overview of this code is that the setInterval() method will run a function that will display a number every second. The number will start at 5, and then when it gets to 0, an alert box will be fired stating that “5 seconds have passed”.

We will then end the setInterval() method from running the function again by using the clearInterval() method. The code clearInterval(interval); will clear the timer set by a setInterval() method.

To view this example in action, take a look at the last example in this post.

Let’s take a look at another example below.

clearInterval() in Action Using JavaScript

In this example, we will have a div with a background color. We will change the background color of the div every second. We will provide the user with a button to stop the changing of the color.

Here is the simple HTML setup:

Stop Background Color Change

In the JavaScript code, we will have a setInterval() method that will run a function every 1000 milliseconds, or 1 second. In the function, we will generate a random color with some JavaScript code. We will then change the background color of #div2 using the backgroundColor property.

We will then use an onclick event to run a stopBackground() function we create when the user clicks a button. This function will simply clear the timer of the setInterval() method we created. We do this by using the clearInterval() method.

Here is the JavaScript code:

var interval = setInterval(function(){
  //Create a random color
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  
  //Set the background of #div2 to this color
  document.getElementById("div2").style.backgroundColor = randomColor;
}, 1000);

//Our function to clear the setInterval() method above
function stopBackground(){
  clearInterval(interval);
}

The final code and output for this example of using the clearInterval() method in JavaScript is below:

Code Output:

Stop Background Color Change

Full Code:

Stop Background Color Change


<script>

var interval = setInterval(function(){
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  document.getElementById("div2").style.backgroundColor = randomColor;
}, 1000);

function stopBackground(){
  clearInterval(interval);
}

</script>

Hopefully this article has been useful for you to understand how the clearInterval() method in JavaScript works.

Learn more here.

Categorized in:

JavaScript,

Last Update: May 3, 2024