setTimeout()

const id = setTimeout(function, ms, arg1, …)

Schedules one call of a function after the given number of milliseconds. The code after setTimeout() continues right away, it does not wait.

Parameters

  • function – the function to call (not a string with code).
  • ms – optional, the delay in milliseconds (default 0).
  • arg1, … – optional, arguments for the function.

Return value

The timer ID for clearTimeout() (a number in the browser, an object in Node.js).

Examples

console.log('1. synchronous');
setTimeout(() => console.log('4. setTimeout 0 ms'), 0);
Promise.resolve().then(() => console.log('3. Promise'));
setTimeout((who) => console.log('5. after 20 ms –', who), 20, 'Jane');
console.log('2. synchronous');

Output:

1. synchronous
2. synchronous
3. Promise
4. setTimeout 0 ms
5. after 20 ms – Jane

Waiting as a Promise – for use with await:

const wait = (ms) => new Promise((done) => setTimeout(done, ms));
const start = Date.now();
await wait(100);
console.log(Date.now() - start >= 95 ? 'waited 100 ms' : 'too early');

Output:

waited 100 ms

Things to watch out for

  • The delay is a minimum, not an exact time – the function waits until the browser finishes its current work. In an inactive browser tab timers slow down (at most once per second or even less often).
  • Even with 0 ms the function runs only after the current code has finished and after Promises have been processed (see the order in the example).
  • A string instead of a function (setTimeout('code()', 100)) runs like eval – do not use it.

Browser support

Baseline – widely available: works in all major browsers and has for a long time (since 2015 at the latest). Since version: Chrome 1, Firefox 1, Safari 1; Node.js 0.10.0. As of October 2026 (MDN browser-compat-data).

Related:
clearTimeout()
setInterval()
Promise
requestAnimationFrame()