Created
November 20, 2021 19:25
-
-
Save dillonstreator/50fe9911b4e9eebe6f67a43ecba2a02c to your computer and use it in GitHub Desktop.
javascript/typescript interval to execute every midnight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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