Timers and Animation in JavaScript

Javascript 9 min min read Updated: Mar 09, 2026 Intermediate
Timers and Animation in JavaScript
Intermediate Topic 15 of 15

JavaScript provides powerful timer functions that allow developers to execute code after a specific delay or repeatedly at fixed intervals. These timers are commonly used for animations, automatic updates, notifications, and dynamic user interface behavior.

The two main timer functions in JavaScript are setTimeout() and setInterval(). These functions help control when and how often certain pieces of code run.

What are JavaScript Timers?

JavaScript timers allow developers to schedule code execution in the future. Instead of running immediately, the code runs after a specified delay or repeatedly over time.

Key Point: JavaScript timers help control when code runs, making animations and delayed actions possible.

setTimeout()

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

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

This message appears after 2 seconds.

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

Clearing setTimeout()

Sometimes developers may want to cancel a timer before it runs. This can be done using clearTimeout().

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

The scheduled timer is cancelled before execution.

setInterval()

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

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

This message repeats every second.

Key Point: setInterval() keeps running until it is stopped manually.

Clearing setInterval()

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

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

The repeating timer stops.

Using Timers for Animation

Timers are often used to create simple animations by updating element properties repeatedly.

javascript let position = 0; let animation = setInterval(function(){ position += 10; console.log("Element moved to position " + position); if(position >= 100){ clearInterval(animation); } }, 500);
Output

The position value increases gradually, simulating movement.

Common Uses of JavaScript Timers

  • Creating animations
  • Displaying notifications after a delay
  • Updating content automatically
  • Running background tasks periodically
Key Point: Timers are widely used in animations, countdown timers, and real-time updates.

Conclusion

JavaScript timers such as setTimeout() and setInterval() allow developers to control when code executes. These functions are essential for creating animations, delays, and repeating tasks in web applications.

By combining timers with DOM manipulation, developers can build dynamic and interactive user experiences.

In the next tutorial, you will learn about Fetch API in JavaScript, which allows JavaScript 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