JavaScript Window setTimeout() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.setTimeout() sets a timer that runs a function once after the delay expires (MDN). It returns a timeout ID — pass that ID to clearTimeout() to cancel. For repeating calls, use setInterval().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.setTimeout. Start with this line.

JavaScript
setTimeout(fn, delay, ...args)
CallWhat it does
setTimeout(fn, delay, ...args)Primary signature.
window.setTimeout(...)Call with the arguments your case needs.

Built into the browser. Call window.setTimeout(...) (or the global setTimeout(...)) — no import required.

Library declarations
JavaScript
setTimeout(fn, delay, ...args)

Parameters

ParameterDescription
funcRequired. Function to run once after the delay
delayOptional. Milliseconds to wait (default 0)
param1…NOptional. Forwarded to func when it runs

Return Value

Return ValueDescription
*Pass to clearTimeout()

JavaScript Examples

Example 1 — Run Once After a Delay

MDN: schedule a callback that runs once when the timer expires.

JavaScript
const id = setTimeout(function () {
  console.log("Hello after 2 seconds");
}, 2000);

// optional: clearTimeout(id);
Try It Yourself

How It Works

  1. Schedule. setTimeout registers the callback.
  2. Returns now. You get an ID immediately (async).
  3. Later. After ~2000 ms the callback runs once.
  4. Cancel option. clearTimeout(id) before it fires.

Example 2 — Pass Extra Arguments

MDN: arguments after delay are forwarded to the callback.

JavaScript
function greet(name) {
  console.log("Hello, " + name);
}

setTimeout(greet, 1000, "Alex");
// or: setTimeout(() => greet("Alex"), 1000);
Try It Yourself

How It Works

  1. Callback. greet expects a name.
  2. Args after delay. "Alex" is passed when it runs.
  3. Arrow form. Wrapping is equally fine.
  4. Once. The greeting prints a single time.

Example 3 — Cancel with clearTimeout

MDN: cancel the timer before the callback runs.

JavaScript
const id = setTimeout(function () {
  console.log("You will not see this if canceled");
}, 3000);

clearTimeout(id);
console.log("Canceled");
Try It Yourself

How It Works

  1. Schedule. Start a 3-second timer.
  2. Clear early. clearTimeout(id) removes it.
  3. No callback. The delayed log never runs.
  4. UX pattern. “Undo” / cancel toast before it appears.

Example 4 — Async Order (Does Not Block)

MDN: shorter delays finish first; code after setTimeout runs immediately.

JavaScript
console.log("A: start");

setTimeout(() => console.log("B: after 0ms"), 0);
console.log("C: after schedule");

// Typical order: A, C, then B
Try It Yourself

How It Works

  1. Schedule. delay: 0 queues for the next turn.
  2. Continue. Sync code after the call still runs first.
  3. Then callback. B runs after the current stack finishes.
  4. Takeaway. Timers never pause the rest of your script.

Example 5 — setTimeout vs setInterval

One runs once; the other repeats until cleared.

JavaScript
setTimeout(() => console.log("timeout once"), 1000);

const i = setInterval(() => console.log("interval"), 1000);
// later: clearInterval(i);
Try It Yourself

How It Works

  1. Timeout. Logs once after 1 second.
  2. Interval. Keeps logging until cleared.
  3. Choose. Toasts / delays → timeout; clocks → interval.
  4. Recursive tip. Chain setTimeout after work finishes if the delay must wait on completion.

Browser Support

Window.setTimeout() is marked Baseline Widely available on MDN (since July 2015). It is not Deprecated, Experimental, or Non-standard. Available in browsers and workers; prefer function callbacks over string code.

✓ Baseline · Widely available

setTimeout()

Run a callback once after a delay across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Universal timer API. Store the ID to cancel; avoid string code; remember delays can be longer under load.

Frequently Asked Questions

It sets a timer that executes a function (or code) once after the delay expires (MDN). It returns a timeout ID immediately so you can cancel with clearTimeout() before the callback runs.
No. MDN marks Window.setTimeout() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Passing a string as code is strongly discouraged (injection / XSS risk).
A positive integer timeout ID. Pass that ID to clearTimeout() to cancel before the timer fires.
No. setTimeout is asynchronous — it schedules the callback and returns right away. Lines after the call run immediately while the timer counts down.
setTimeout runs once after a delay. setInterval repeats until cleared. Use setTimeout for one-shot delays; use setInterval (or recursive setTimeout) for repeating work.
Yes. Extra arguments after the delay are forwarded: setTimeout(greet, 2000, "Alex"). Or wrap the call: setTimeout(() => greet("Alex"), 2000).

Next: stop()

Stop the browser from loading the current document.

stop() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful