JavaScript Timers

setTimeout() and setInterval() allow you to register a function
to be invoked once or repeatedly after a specified amount
of time has elapsed. These are important global functions of
client-side JavaScript, and are therefore defined as methods of
Window, but they are general-purpose functions and don’t
really have anything to do with the window.
The setTimeout() method of the Window object schedules a
function to run after a specified number of milliseconds elapses.
setTimeout() returns a value that can be passed to clear
Timeout() to cancel the execution of the scheduled function.




If you call setTimeout() with a time of 0 ms, the function you
specify is not invoked right away. Instead, it is placed on a
queue to be invoked “as soon as possible” after any currently
pending event handlers finish running.
setInterval() is like setTimeout() except that the specified
function is invoked repeatedly at intervals of the specified
number of milliseconds:
// Call updateClock() every 60 seconds
setInterval(updateClock, 60000);
Like setTimeout(), setInterval() returns a value that can be
passed to clearInterval() to cancel any future invocations of
the scheduled function.