The setInterval
method can be used to
repeatedly run a function at
fixed time interval.
A basic setInterval
method has two parameters:
-
The function that needs to be run
-
The time delay in milliseconds
The example given below
prints "Hello, World!" on
the console after every
5 seconds.
You can also pass arguments to the function,
by adding them after the first
two parameters of setInterval
.
Similar to the preferred way
of passing parameters
discussed in the chapter introducing setTimeout
,
let's use a different way of
passing parameters.
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.