JavaScript Timing Events with setTimeout and setInterval

Learn JavaScript timing events with setTimeout(), setInterval(), clearTimeout(), clearInterval(), and practical examples for delayed code.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

JavaScript Timing Events with setTimeout and setInterval

JavaScript timing events let you schedule code to run after a delay or at repeated intervals. The two main timer APIs are setTimeout() for one delayed execution and setInterval() for repeated execution.

Use clearTimeout() and clearInterval() when a timer should stop before it runs again. For a deeper look at one-shot timers, see JavaScript setTimeout().

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Quick reference

API Role
setTimeout(fn, ms) Run fn once after at least ms milliseconds
setInterval(fn, ms) Run fn repeatedly until cleared
clearTimeout(id) / clearInterval(id) Cancel a pending timer

Method 1: Run code once with setTimeout

setTimeout() runs a callback after a delay.

javascript
function later(ms, value) {
  return new Promise((resolve) => setTimeout(() => resolve(value), ms));
}

later(5, "done").then((value) => {
  console.log("timeout-value:", value);
});
Output

You should see one line logging timeout-value: done.

This is useful for delayed UI updates, short retries, and scheduled follow-up work.


Method 2: Repeat code with setInterval

setInterval() repeats the callback until it is cancelled.

javascript
let count = 0;
const id = setInterval(() => {
  count++;
  if (count === 3) {
    clearInterval(id);
    console.log("interval-fired:", count);
  }
}, 5);
Output

You should see one line logging interval-fired: 3.

Use setInterval() only when repeated execution is the real requirement.


Method 3: Cancel a timeout or interval

clearTimeout prevents the callback from ever running if the delay has not elapsed yet.

javascript
const timeoutId = setTimeout(() => console.log("will not run"), 50);
clearTimeout(timeoutId);
console.log("cleared:", timeoutId != null);
Output

You should see one line logging cleared: true. You should not see will not run because the timeout was cleared before it fired.

Use clearInterval() for intervals and clearTimeout() for timeouts.


Method 4: Understand Delay and Scheduling

Timers do not guarantee exact millisecond precision. The callback runs when the event loop is free and the delay has elapsed.

This is why timing events are good for scheduling, animations, and retries, but not for precise measurement.


Common Questions About JavaScript Timing Events

What is the difference between setTimeout and setInterval?

setTimeout() runs once after a delay. setInterval() runs repeatedly until cancelled.

Does setTimeout block JavaScript?

No. It schedules work and lets the rest of the code continue.

Can I stop a scheduled timer before it runs?

Yes. Store the timer ID and pass it to clearTimeout() or clearInterval() as appropriate.

When should I use clearTimeout and clearInterval?

Use them when a scheduled callback should stop before it runs again or before it runs at all.


Summary

JavaScript timing events are built around setTimeout() and setInterval(). Use setTimeout() for one delayed callback, setInterval() for repeated callbacks, and the matching clear methods when a scheduled timer must be cancelled. The actual timing can drift slightly depending on the runtime and browser state, so pair these APIs with the dedicated JavaScript setTimeout() guide when you need a deeper timer breakdown.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …