user4022619
user4022619

Reputation:

Nodejs Repeat every system hour

I am using moment (for current time) and waitjs (repeat) modules.
If i write the codes as the below:

repeat(86400000, function() {  //DASHBOARD 
});

This function repeat every 1 hour. But it has counted since the program started.
I want to work with system time. For example; when the system time 1:00:00 (A.M.) the function will be work. And continues every hour.
How can i do this?

THANKS!

Upvotes: 0

Views: 645

Answers (2)

cgross
cgross

Reputation: 1952

Using Moment.js and waitjs you would do something like this:

// Wait until end of hour
wait(moment().endOf('hour') - moment(), function() {
    // Start executing repeat function
    repeat(86400000, function() {  //DASHBOARD 
    });
});

Check out Moment.js endOf

Upvotes: 1

Tom Grant
Tom Grant

Reputation: 2055

On program startup you can get the time until the next full hour, so for example:

  • If the program starts at 1:20AM
  • Time until the next full hour would be forty minutes.

Use this time to delay the execution of the first function call, this call be done with setTimemout (http://nodejs.org/api/timers.html). Following the example you would set a delay of forty minutes, and then the function would execute at 2:00AM.

You can then in this executed function, setup your repeat function to repeat every full hour.

Upvotes: 1

Related Questions