Time
Last updated
Was this helpful?
Last updated
Was this helpful?
Usually we think about programs as something recipes that execute sequentially. However, sometimes we wish to wait or repeat something at different times.
The setTimeout
function lets us schedule something to be executed at a later time.
Imagine you were responsible for the launch of SpaceX Falcon 9 rocket. Your program has to call the liftOff
function exactly on the 1 minute mark.
The first argument to setTimeout
is the function that is to be executed, the second argument is when that function should be executed. Notice that the last argument is provided as milliseconds.
The setInterval
function allows you to do something at a repeated interval.
Note that the function you schedule is not executed immediately, but executed at the first interval.