javascript - Recurring function to reset variable/counter at midnight (moment.js, node.js) -


i want reset variable during midnight. every night. i'm trying build function moment.js node can't seem recurring part work properly.

this got far.

// calculate time midnight function timetomidnight(){     var midnight = new date();     midnight.sethours(0,0,0,0);      var = new date();     var mstomidnight = midnight - now;     console.log(' ' + mstomidnight + 'ms until midnight');     return mstomidnight; };  // reset counter @ midnight settimeout(function(){     console.log("midnight, something"); }, timetomidnight()); 

how can best make recurring @ midnight, every night?

thanks in advance.

if you're using moment, consider instead implementation

var moment = require('moment');  function timetomidnight() {    var = new date();    var end = moment().endof("day");     return end - + 1000; } 

like function, takes now milliseconds , calculates number of milliseconds until midnight, supported directly when using moment, nice. add 1 second (1000 milliseconds) next day.

a typical pattern function call after timeout.

function roundmidnight() {    console.log('at midnight');    settimeout(roundmidnight,timetomidnight()); }  settimeout(roundmidnight,timetomidnight()); 

pretty generic, in fact depending on value returned, schedule anytime, pretty useful, seem must have thought of that.

node-schedule

a cron-like , not-cron-like job scheduler node.

and did. maybe want node-schedule. looks it's not actively developed now, though.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -