JavaScript setTimeout and setInterval
delaying and repeating events
At times, we may wish to
delay an event (JavaScript has something called
setTimeout
)repeat an event at regular intervals (there's also
setInterval
)
Both methods take in two parameters: the function and the delay (in milliseconds):
setTimeout
setTimeout
With setTimeout
, we insert whatever function we want to delay into the first parameter, then specify the length of that delay (in milliseconds) into the second parameter:
setInterval
setInterval
The setInterval
function follows the same shape as setTimeout
:
clearInterval
clearInterval
We may also stop the interval after a given amount of time by passing it into a clearInterval
within a setTimeout
:
Last updated