---
title: "What is JS timing"  
description: "What is JS timing"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93767/what-is-js-timing  
category: "web application"  
tags: ["javascript"]  
reading_time: 3 minutes  

---

# What is JS timing

What is JS [timing](https://answers.mindstick.com/qa/97195/what-is-trcd-timing)

## 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.

1. setTimeout()
2. 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>
```

**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>
```

\

### 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.** That is, only one code can be interpreted at a time, but it provides us with two such facilities, Timeouts and Intervals, using which we can do the scheduling of the execution of our codes. There are two major methods of experimenting with JavaScript.

1. setInterval()
2. setTimeout()

Timeout is a feature in which our JavaScript code is automatically executed after a specified amount of time. Whereas with the Intervals facility, we can execute a code repeatedly at the interval of a certain time period. We can use the setTimeout() method of the window object of the web browser to automatically execute a code after a certain period of time. This method accepts two arguments as parameters, where the first argument is the code that is to be executed, We can create the animation through the setInterval () method.

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.


---

Original Source: https://answers.mindstick.com/qa/93767/what-is-js-timing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
