node.js reliable microtime setTimeout?

141 Views Asked by At

How do I shoot an event EXACTLY after given time in milliseconds? Is there any module for that? I was looking for it on google, but didn't find anything satisfactory...

1

There are 1 best solutions below

1
peteb On BEST ANSWER

You can't control exact execution of code in the Event Loop. If you need this, then you should look at using a different Framework/Language.

Understanding the Node.js Event Loop, Timers and process.nextTick()

There are no guarantees of when setTimeout() will run, only a guaranteed minimum of how long it will wait, see below excerpt from the above guide:

setTimeout() schedules a script to be run after a minimum threshold in ms has elapsed.

The closest you have is process.nextTick() and even then you're at the mercy of the Event Queue because other things queued with process.nextTick() can occur before yours. This is also dangerous due to possible starvation of the Event Loop if not implemented correctly.