简体   繁体   中英

In Node.js, is setTimeout reliable?

I need to perform many "setTimeouts" 60 seconds. Basically, I'm creating a database record, and 60 seconds from now, I need to check whether the database record was changed.

I don't want to implement a "job queue" since it's such a simple thing, and I definitely need to check it around the 60 second mark.

Is it reliable, or will it cause issues?

When you use setTimeout or setInterval the only guarantee that you get is that the code will not be executed before the programmed time.

It can however start somewhat later because other code that is being executed when the clock ticks (in other words other code will not be interrupted in the middle of the handling of an event to process a timeout or interval event).

If you don't have long blocking processing in your code it means that timed events will be reasonably accurate. If you are instead using long blocking calls then probably node is not the correct tool (it's designed around the idea of avoiding blocking "synch" calls).

you should try WorkerTimer.js it is more good for handling background processes and more accurate than the traditional setInterval or Timeout.

it is available as a node.js npm package. https://www.npmjs.com/package/worker-timer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM