JavaScript setTimeout and setInterval

Javascript 8 min min read Updated: Mar 09, 2026 Intermediate
JavaScript setTimeout and setInterval
Intermediate Topic 4 of 15

JavaScript provides built-in timer functions that allow developers to delay the execution of code or run code repeatedly at specific intervals. Two important timer functions in JavaScript are setTimeout() and setInterval().

These functions are widely used in web development for creating animations, showing notifications, refreshing content, and running background tasks.

What is setTimeout()?

The setTimeout() function executes a piece of code once after a specified delay. The delay is measured in milliseconds.

Key Point: setTimeout() runs a function only once after the given delay.

Syntax of setTimeout()

javascript setTimeout(function, delay);
  • function – The code that will execute.
  • delay – Time in milliseconds before the function runs.

Example of setTimeout()

javascript setTimeout(function(){ console.log("Hello after 2 seconds"); }, 2000);
Output

Hello after 2 seconds

In this example, the message will appear after a delay of 2000 milliseconds (2 seconds).

Stopping setTimeout()

A timer created using setTimeout() can be cancelled using clearTimeout().

javascript let timer = setTimeout(function(){ console.log("This will not run"); }, 3000); clearTimeout(timer);
Output

The timer is cancelled before execution.

What is setInterval()?

The setInterval() function executes a piece of code repeatedly at a fixed time interval.

Key Point: setInterval() runs a function repeatedly until it is stopped.

Syntax of setInterval()

javascript setInterval(function, interval);
  • function – The code to execute repeatedly.
  • interval – Time in milliseconds between each execution.

Example of setInterval()

javascript setInterval(function(){ console.log("Running every second"); }, 1000);
Output

Running every second

Stopping setInterval()

To stop a repeating timer, developers use clearInterval().

javascript let interval = setInterval(function(){ console.log("Processing..."); }, 1000); clearInterval(interval);
Output

The repeating timer stops.

Difference Between setTimeout() and setInterval()

  • setTimeout() runs the function only once after a delay.
  • setInterval() runs the function repeatedly at fixed intervals.
  • setTimeout() is used for delayed execution.
  • setInterval() is used for repeated tasks.
Key Point: setTimeout() executes once, while setInterval() executes repeatedly.

Conclusion

setTimeout() and setInterval() are essential JavaScript timer functions used to control when code runs. They help developers create delays, animations, automatic updates, and background tasks.

Understanding these timers is important for working with asynchronous behavior and dynamic user interfaces in modern web applications.

In the next tutorial, you will learn about JavaScript Fetch API, which allows developers to retrieve data from servers using HTTP requests.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators