crankshaft
crankshaft

Reputation: 2677

nodejs - every minute, on the minute

How can I wait for a specific system time before firing ?

I want to fire an event when seconds = 0, i.e. every minute

while (1==1) {
    var date = new Date();
    var sec = date.getSeconds();
    if(sec===0) {
        Do Something()
    }

}

Upvotes: 3

Views: 6715

Answers (1)

gustavohenke
gustavohenke

Reputation: 41440

You shouldn't do that, because with this while you will have a blocking operation. Also, there are better things to do in any JavaScript platform, like using setInterval/setTimeout functions.
The node docs for them are here.

A quick example of how to achieve what you want:

setInterval(function() {
  var date = new Date();
  if ( date.getSeconds() === 0 ) {
    DoSomething();
  }
}, 1000);

For a more fine grained control over scheduled processes in Node, maybe you should checkout node-cron.

Upvotes: 11

Related Questions