setInterval() Function

The setInterval() function creates and schedules a repeating timer.

The timer will call func at or immediately after the specified delay has elapsed; the timing is not guaranteed to be millisecond-precise. To cancel the callback, call clearInterval(), or call Timer.cancel() on the object returned when the timer is created.

Syntax

setInterval(func, delay)

Parameters

Return value

A Timer object is returned. This can be used to cancel the timer.

Note: the return value is an object, rather than the integer ID returned by Javascript. However, the APIs should be compatible.

Example

> timer = setInterval(() => console.log("hello world"), 100);
undefined
hello world
hello world
hello world
> clearInterval(timer)
undefined

References