Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Last active July 21, 2016 19:51
Show Gist options
  • Save johnmurch/21e09f2032fde79b9818edf24011259e to your computer and use it in GitHub Desktop.
Save johnmurch/21e09f2032fde79b9818edf24011259e to your computer and use it in GitHub Desktop.
Script for running something every hour on the hour no matter when it starts
var now = new Date();
var delay = 60 * 60 * 1000; // 1 hour in msec
console.log('now',new Date().toISOString());console.log('delay',delay,'min',(60 -now.getMinutes()),'sec',(60-now.getSeconds()),'mill',(1000-now.getMilliseconds()));
var start = delay - (now.getMinutes() * 60 + now.getSeconds()) * 1000; //msec until next hour
console.log('start',start);
var kickoff = new Date();
kickoff.setMilliseconds(start);
console.log('kickoff', new Date(kickoff).toISOString());
setTimeout(function timeToNextHour() {
tick();//First Run (as start is the time left until the next hour)
setInterval(tick, delay);//Each Hour There After on the hour
}, start);
function tick(){
console.log(new Date().toISOString());
console.log('foo')
console.log('bar')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment