Skip to content

Instantly share code, notes, and snippets.

@dillonstreator
Created November 20, 2021 19:25
Show Gist options
  • Save dillonstreator/50fe9911b4e9eebe6f67a43ecba2a02c to your computer and use it in GitHub Desktop.
Save dillonstreator/50fe9911b4e9eebe6f67a43ecba2a02c to your computer and use it in GitHub Desktop.
javascript/typescript interval to execute every midnight
const midnightInterval = (fn: ()=>void) => {
const midnight = new Date();
midnight.setHours( 24 );
midnight.setMinutes( 0 );
midnight.setSeconds( 0 );
midnight.setMilliseconds( 0 );
const msUntilMidnight = ( midnight.getTime() - new Date().getTime() ) / 1000;
setTimeout(() => {
fn()
setInterval(fn, 1000 * 60 * 60 * 24)
}, msUntilMidnight)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment