setTimeout() (JavaScript)

setTimeout() schedules a function to run once after a given number of milliseconds. It is the standard way to delay code in JavaScript.

JavaScript

Pass a callback and a delay in milliseconds; JavaScript continues running other code and fires the callback once the delay passes. The delay is a minimum, not a guarantee.

setTimeout returns an id you can pass to clearTimeout() to cancel it before it runs.

Syntax

setTimeout(callback, milliseconds)

Examples

JavaScript

setTimeout(() => {
  console.log("Ran after 1 second");
}, 1000);

Output:

Ran after 1 second

See also

Back to the Dictionary