setInterval()
The setInterval()
method, repeatedly
calls a function or executes a code snippet, with a fixed time delay between each
call.
This method returns an interval ID which uniquely identifies the interval, so you
can remove it later by calling clearInterval()
.
Syntax
setInterval(code)
setInterval(code, delay)
setInterval(func)
setInterval(func, delay)
setInterval(func, delay, arg0)
setInterval(func, delay, arg0, arg1)
setInterval(func, delay, arg0, arg1, /* … ,*/ argN)
Parameters
func
- : A
function
to be executed everydelay
milliseconds. The first execution happens afterdelay
milliseconds.
- : A
code
- : An optional syntax allows you to include a string instead of a function, which is
compiled and executed every
delay
milliseconds. This syntax is not recommended for the same reasons that make using `eval() a security risk.
- : An optional syntax allows you to include a string instead of a function, which is
compiled and executed every
delay
optional- : The time, in milliseconds (thousandths of a second), the timer should delay in
between executions of the specified function or code. Defaults to 0 if not specified.
below for details on the permitted range of
delay
values.
- : The time, in milliseconds (thousandths of a second), the timer should delay in
between executions of the specified function or code. Defaults to 0 if not specified.
below for details on the permitted range of
arg0, …, argN
optional- : Additional arguments which are passed through to the function specified by func once the timer expires.
Return value
The returned intervalID
is a numeric, non-zero value which identifies the
timer created by the call to setInterval()
; this value can be passed to
clearInterval()
to cancel the interval.
It may be helpful to be aware that setInterval()
and
setTimeout()
share the same pool
of IDs, and that clearInterval()
and
clearTimeout()
can technically
be used interchangeably. For clarity, however, you should try to always match them to
avoid confusion when maintaining your code.
Note: The
delay
argument is converted to a signed 32-bit integer. This effectively limitsdelay
to 2147483647 ms, since it's specified as a signed integer in the IDL.