Learn JavaScript
Timeouts and Intervals
28.3
Lesson

setInterval method

setInterval() method can be used to repeatedly run a function at fixed time intervals.

A basic setInterval() method has 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 every 5 seconds.

You can also pass arguments to the function, by adding them after the first two parameters of the setTimeout() method call.

The setInterval() method is very similar to the setTimeout() method, with the only difference being that setTimeout() runs the function only once after the time delay, while setInterval() runs the function repeatedly after a specified time interval.