SetInterval()


SetInterval()

The setInterval() method is used to schedule a reoccurring task to be put on the event queue every time a given number of milliseconds elapses. The first parameter to setInterval() is the callback function that is going to be executed. The second parameter is the amount of time to wait before the reoccurring task is put back on to the event queue.

Notice how the setInterval() method is used to log a number every second:



ClearInterval()

The clearInterval() method is used to stop an interval timer set by setInterval(). The setInterval() method call returns a numeric timerID that is used to identify the interval timer. This timerID can be passed into the clearInterval() method call to stop the interval timer.

Notice how clearInterval() is used to stop an interval from continuing after it executes three times:

results matching ""

    No results matching ""