Skip to content

Instantly share code, notes, and snippets.

@dayaki
Created July 21, 2022 13:22
Show Gist options
  • Save dayaki/cb0346a6acb93f5b500dbec5b87d0fef to your computer and use it in GitHub Desktop.
Save dayaki/cb0346a6acb93f5b500dbec5b87d0fef to your computer and use it in GitHub Desktop.
Reset a variable daily
let myVar = 10;
function scheduleReset() {
// get current time
let reset = new Date();
// update the Hours, mins, secs to the 24th hour (which is when the next day starts)
reset.setHours(24, 0, 0, 0);
// calc amount of time until restart
let t = reset.getTime() - Date.now();
setTimeout(function() {
// reset variable
myVar = 1;
// schedule the next variable reset
scheduleReset();
}, t);
}
scheduleReset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment