Clearing an Interval in JavaScript

Posted on Thursday, April 2, 2020

In JavaScript, we have a method available to us on the window object (or global, in Node) called setInterval. If you are unfamiliar with this method, it is similar in nature to setTimeout, which is much more commonly used. If you haven't heard of either or just want a refresher, here's what they do:


The window.setTimeout (or global.setTimeout in Node) method accepts two parameters: a callback function to run, and a delay (in milliseconds) to wait until running that function. This is handy for simulating asynchronous code in mockups or examples if you don't have a real API endpoint to use yet. Here's an example:

1setTimeout(function() {
2  console.log('World')
3}, 2000)

When the code above executes, the word "Hello" will be printed to the console, and then 2 seconds later (2000 milliseconds) the word "World" will be printed to the console.


The setInterval method looks very similar. It also takes a callback function and an a delay/interval parameter. The main difference is that instead of running the callback function once after the provided delay, it continuously runs the function. So if we have this:

1setInterval(function() {
2  console.log('World')
3}, 2000)

This will print "Hello" to the console and then 2 seconds later it will print "World", just like it did above. Except now it will continue printing "World" every 2 seconds until the page is reloaded or you close the tab.

This method can be super handy for a number of things. Let's look at a simple example. You are building a quiz app and the user has 10 seconds to answer a question. If they answer within the time, you move on to the next question and reset the timer. If the timer reaches zero, they lose. You might do something like this:

1let quizTimer = 10
2let status = 'playing'
4setInterval(() => {
5  quizTimer -= 1
7  if (quizTimer === 0) {
8    // they lose
9    status = 'lose'
10  }
11}, 1000)
13let realAnswer = 'blue'
14checkAnswer(answer) {
15  if (answer === realAnswer) {
16    // they got it right. reset the timer to 10 seconds
17    quizTimer = 10
18  } else {
19    status = 'lose'
20  }

This is fairly crude but you get the idea. We decrement the timer every second and if it hits zero, they lose. If they get the question right, we reset it to 10 seconds. If they answer the question wrong, they also lose.

But then you have a little bug... the quizTimer keeps decrementing even after the game is over, because your interval is still running. That's no good. So how do we get rid of the interval?


There's a method called clearInterval available to us as well. Its method signature takes one parameter, which is an instance of setInterval. Here's how it works:

1let quizTimer = 10
2let quizInterval = setInterval(() => {
3  quizTimzer -= 1
5  if (quizTimer === 0) {
6    clearInterval(quizInterval)
7  }
8}, 1000)

We do two main things here. First, instead of simply invoking setInterval, we set it equal to a variable. That way we have a reference to the instance of the interval. Second, when our timer hits zero, we pass that instance as the parameter into the clearInterval method. Now our timer will stop at zero and the interval will be done.

If you're not interested in Vue.js, the blog post ends here :)

Usage in Vue.js

I ran into this issue of how to clear an interval in Vue a couple years ago. Quite recently I had a colleague ask for help trying to figure it out, and earlier that same day I was on a Vue forum where someone asked the same exact question as my colleague - "How do I stop a setInterval from running inside a Vue component?"

It's not the most intuitive thing to figure out, so we'll cover it here as well.

2  <div>
3    <p>
4      {{ quizTimer }} second(s) remaining
5    </p>
6    <button
7      @click="doCountdown">
8      Start Quiz
9    </button>
10  </div>
1export default {
2  data() {
3    quizTimer: 10,
4    quizInterval: null
5  },
6  methods: {
7    doCountdown() {
8      this.quizInterval = setInterval(() => {
9        this.decrementTimer()
10        if (this.quizTimer === 0) {
11          this.stopTimer()
12        }
13      }, 1000)
14    },
15    decrementTimer() {
16      this.quizTimer -= 1
17    },
18    stopTimer() {
19      clearInterval(this.quizInterval)
20    }
21  }

Okay, so what's going on here? We have a data property called quizTimer that starts at 10 and a quizInterval that defaults to null. We have a paragraph tag in our template that renders the number of seconds remaining, and we have a button in our template that, when clicked, starts the countdown interval.

Inside this doCountdown method, we start the interval and set it to the quizInterval property on our data. The interval begins running and it decrements the value of the timer by 1 every second. If the value is zero after decrementing, it stops the timer by running clearInterval and passing in the interval instance that we have stored in our data property.

Once you see it laid out, it's not really all that different from how you would normally do it. The main thing that I think isn't super obvious is that you need a way to access the interval in order to clear it. In Vue, variables declared in one method aren't available in other methods, just like function scoping. You could declare a variable outside the default export and reference it anywhere, but that's not typically considered best practice. Putting it in data means you get access to it from another method.

Another approach that would also work is using a local variable inside of doCountdown and clearing it within itself, like we did above:

1export default {
2  data() {
3    quizTimer: 10
4  },
5  methods: {
6    doCountdown() {
7      let timer = setInterval(() => {
8        this.decrementTimer()
9        if (this.quizTimer === 0) {
10          clearInterval(timer)
11        }
12      }, 1000)
13    },
14    decrementTimer() {
15      this.quizTimer -= 1
16    }
17  }

This approach would absolutely work, but what you lose is the ability to clear the timer from outside the scope of the doCountdown method. By putting the interval in a property on the data, we could put a button in our template that allowed us to stop the timer if, say, we needed a way to "pause" our quiz:

2  <div>
3    <p>
4      {{ quizTimer }} second(s) remaining
5    </p>
6    <button
7      @click="doCountdown">
8      Start Quiz
9    </button>
11    <button
12      @click="stopTimer">
13      Pause Quiz
14    </button>
15    <button
16      @click="doCountdown">
17      Resume Quiz
18    </button>
19  </div>

Hopefully this was a helpful explanation!

1setInterval(() => {
2  console.log("Happy coding!")
3}, 1000)