Skip to content

Instantly share code, notes, and snippets.

@danbrianwhite
Created February 15, 2017 13:15
Show Gist options
  • Save danbrianwhite/2b5440499c46eedfe7eebe40e3b897a7 to your computer and use it in GitHub Desktop.
Save danbrianwhite/2b5440499c46eedfe7eebe40e3b897a7 to your computer and use it in GitHub Desktop.
Refresh every x seconds on the exact modulus second
function refreshEvery(secs) {
secs = typeof secs === 'undefined' && secs < 1 ? 1 : secs;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
seconds = seconds + (secs - (seconds % secs))
var then = new Date();
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);
then.setMilliseconds(0);
var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment