Timers are used to schedule events in the future.
They are used when one seeks to delay the execution of some block of code until a specified number of milliseconds have passed, to schedule periodic execution of a particular function, and so on. JavaScript provides two asynchronous timers: setInterval() and setTimeout() .
The key takeaway will be this: when using timers, one should make no assumptions about the amount of actual time that will expire before the callback registered for this timer fires, or about the ordering of callbacks. Node timers are not interrupts. Timers simply promise to execute as close as possible to the specified time (though never before), beholden, as with every other event source, to event loop scheduling.
setTimeout
Timeouts can be used to defer the execution of a function until some number of milliseconds into the future. Consider the following code: setTimeout(a, 1000); setTimeout(b, 1001); One would expect that function b would execute after function a . However, this cannot be guaranteed — a may follow b , or the other way around.
[N]ode uses a single low level timer object for each timeout value. If you attach multiple callbacks for a single timeout value, they'll occur in order, because they're sitting in a queue. However, if they're on different timeout values, then they'll be using timers in different threads, and are thus subject to the vagaries of the [CPU] scheduler."