---
title: "What is use of “setTimeout” and “setTimeinterval” in JS?"  
description: "What is use of “setTimeout” and “setTimeinterval” in JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93768/what-is-use-of-settimeout-and-settimeinterval-in-js  
category: "web application"  
tags: ["javascript"]  
reading_time: 4 minutes  

---

# What is use of “setTimeout” and “setTimeinterval” in JS?

What is use of “setTimeout” and “setTimeinterval” in JS?

## Answers

### Answer by user

Programmers use timing events to delay the execution of certain code, or to repeat code at a specific interval. There are two native functions in the JavaScript library used to accomplish these tasks. • setTimeout() • setInterval(). **setTimeout** setTimeout() is used to delay the execution of the passed function by a specified amount of time. There are two parameters that you pass to setTimeout(): the function you want to call, and the amount of time in milliseconds to delay the execution of the function. setTimeout() will execute the function from the first argument one time after the specified time has elapsed.

```
Syntax – setTimeout(function-name , millisecond)
```

```
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>Click 'Try it'. Wait 3 seconds, and the page will alert 'Hello'.</p>
<button onclick='setTimeout(myFunction, 3000);'>Try it</button>
<script>
function myFunction() {
  alert('Hello');
}
</script>
</body>
</html>
```

in this example show an alert box after 3 second. **setInterval** Use setInterval() to specify a function to repeat with a time delay between executions. Again, two parameters are passed to setInterval(): the function you want to call, and the amount of time in milliseconds to delay each call of the function. setInterval() will continue to execute until it is cleared.

```
Syntax – setInterval (function-name , millisecond)
```

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
    <h2>JavaScript Timing</h2>
    <p>A script on this page starts this clock:</p>
    <p id='demo'></p>
    <script>
        function myTimer() {
          const d = new Date();
          document.getElementById('demo').innerHTML = d.toLocaleTimeString();
        }
        setInterval(myTimer, 1000);
    </script>
</body>
</html>
```

this example show a digital watch increment the every second .

### Answer by Ethan Karla

The window timing object allows the execution of code at specified time intervals. These time intervals are called time events. **JavaScript is a Single Threaded Language.** Both setTimeout() and setInterval() methods return a unique ID of their own while executing. By storing this ID in a variable, those IDs are passed as parameters to the clearTimeout() or clearInterval() method. Both of these methods prevent those codes from being executed, whose ID is passed in as a parameter.

## setTimeout()

The setTimeout() method is used to delay the execution of a process or thread. Timeout is a feature in which our JavaScript code executes automatically after a certain amount of time. This method accepts two arguments as parameters, where the first argument is the code that is to be executed, while the second argument is the time period in milliseconds after which the code specified as the first argument is executed.

SyntaxsetTimeout( duration ) // duration value in millisecondsetTimeout( function, durataion)setTimeout( statement, duration )

## Example

setTimeout('console.log('Hello')', 3000); or window.setTimeout('console.log('Hello')', 3000);

'Hello' is not printed immediately in the console window, but 'Hello' is printed after 3 seconds of running this code. That is, by the setTimeout() method, we can do Time Scheduling of the code to be executed.

## setInterval()

On executing the code, it prints the message 'Hello' after every second in the code console window and continues to run the same code again and again, until we reload the current web page or web Do not close the browser. Generally, different types of animations are created in JavaScript using both these methods.

Syntax setInterval( duration ) // duration value in millisecond setInterval( function, durataion) setInterval( statement, duration )

## Example

setInterval('console.log('Hello')', 3000); or window.setInterval('console.log('Hello')', 3000);

### Answer by Anubhav Sharma

You had a typo in your fiddle, if works just fine, but instead of 5000 ms you had 55000ms set for the timeout.

```javascript
 setTimeout(function () {
   clearInterval(movingbulldozer);
 }, 5000);
```


---

Original Source: https://answers.mindstick.com/qa/93768/what-is-use-of-settimeout-and-settimeinterval-in-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
