TIMEX
TIMEX

Reputation: 271584

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?

Upvotes: 4

Views: 1688

Answers (2)

6502
6502

Reputation: 114461

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

Upvotes: 7

Adarsh Hegde
Adarsh Hegde

Reputation: 631

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

Upvotes: 1

Related Questions