Learn JavaScript
Timeouts and Intervals
28.1
Lesson

setTimeout method

setTimeout() can be used to run a function after a certain time delay.

A basic setTimeout() method takes two parameters:

  1. The function that needs to be run
  2. The time delay in milliseconds

The example given below prints Hello, World! onto the console after a delay of 3 seconds.

If the function that you are passing to setTimeout needs arguments, you can pass them by adding them after the first two parameters of the setTimeout() method call as shown below:

Also, the setTimeout() method returns a number value. This value can be used to cancel the timeout later if needed.