---
title: "What are use of “clearTimeout” and “clearInterval” in JS?"  
description: "What are use of “clearTimeout” and “clearInterval” in JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93769/what-are-use-of-cleartimeout-and-clearinterval-in-js  
category: "web application"  
tags: ["javascript"]  
reading_time: 3 minutes  

---

# What are use of “clearTimeout” and “clearInterval” in JS?

What are use of “clearTimeout” and “clearInterval” in JS?

## Answers

### Answer by user

The **clearTimeout()** method clears a timer set with the setTimeout() method. The ID value returned by setTimeout() is used as the parameter for the clearTimeout() method.

```
myVar = setTimeout('javascript function', milliseconds);
```

Then, if the function has not already been executed, you will be able to stop the execution by calling the clearTimeout() method.

```
Example
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
    <button onclick='myFunction()'>Try it</button>
    <button onclick='myStopFunction()'>Stop the alert</button>
    <p>this is show alert box in 3 second, if you want stop then click 'stop alert button'</p>
    <script>
        var myVar;
        function myFunction() {
            myVar = setTimeout(function () {
                alert('this alert show within 3 second ');
            }, 3000);
        }
        function myStopFunction() {
            clearTimeout(myVar);
        }
    </script>
</body>
</html>
```

The **clearInterval()** method clears a timer set with the setInterval() method. The ID value returned by setInterval() is used as the parameter for the clearInterval() method. To be able to use the clearInterval() method, you must use a variable when creating the interval method.

```
myVar = setInterval('javascript function', milliseconds);
```

Then you will be able to stop the execution by calling the clearInterval() method. clearInterval(myVar);

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds, which will toggle between two background colors.</p>
<button onclick='stopColor()'>Stop Toggling</button>
<button onclick='startColor()'>start Toggling</button>
<script>
    var myVar = setInterval(setColor, 10);
    function setColor() {
        var x = document.body;
        x.style.backgroundColor = x.style.backgroundColor == 'red' ? 'yellow' : 'red';
    }
    function stopColor() {
        clearInterval(myVar);
    }
    function startColor()
    {
        myVar = setInterval(setColor, 10);
    }
</script>
</body>
</html>
```

\

### Answer by Ethan Karla

The setTimeout() and setInterval() methods return a unique id of our code and unless we store that unique id in a variable, we can prevent this code by passing it as a parameter to the clearTimeout() or clearInterval() method. can. If in a specific situation we want to stop the above code from being executed, then we can do this work by using clearTimeout() and clearInterval() methods.

## clearTimeout()

```
SyntaxclearTimeout() // clear the current timeout variableclearTimeout( time out variable ) // claer the given  variable
```

### Example

```
var id1 = setTimeout(function(){console.log('Hello')}, 3000);
clearTimeout(id1);
```

## clearInterval()

```
Syntax
clearInterval() // clear the current interval variable
clearInterval( time out variable ) // claer the given interval variable 
```

Example

```
var id2 = setInterval(function(){console.log('Hello')}, 1000);
clearInterval(id2);
```

\


---

Original Source: https://answers.mindstick.com/qa/93769/what-are-use-of-cleartimeout-and-clearinterval-in-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
