node.jsmicrotime

node.js reliable microtime setTimeout?


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...


Solution

  • 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.