Created
September 29, 2017 15:20
-
-
Save Fraasi/d8dfe3eb49b587636329ddb8d55e6738 to your computer and use it in GitHub Desktop.
Simple timer to run my bot once everyday
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
function msToTime(duration) { | |
// from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript | |
var milliseconds = parseInt((duration%1000)/100) | |
, seconds = parseInt((duration/1000)%60) | |
, minutes = parseInt((duration/(1000*60))%60) | |
, hours = parseInt((duration/(1000*60*60))%24); | |
hours = (hours < 10) ? "0" + hours : hours; | |
minutes = (minutes < 10) ? "0" + minutes : minutes; | |
seconds = (seconds < 10) ? "0" + seconds : seconds; | |
return hours + ":" + minutes + ":" + seconds + "." + milliseconds; | |
} | |
function msUntillCodeRuns() { | |
var now = new Date(); | |
var then = new Date(now); | |
then.setHours(23, 0, 0, 0); //when to run | |
if (then - now <= 0) then.setDate(now.getDate() + 1); | |
var msToRunTime = then - now; | |
console.log(`${msToTime(msToRunTime)} to runtime.`); | |
return msToRunTime; | |
} | |
function codeToRun() { | |
console.log("Code runs...") | |
setTimeout(codeToRun, msUntillCodeRuns()); | |
} | |
setTimeout(codeToRun, msUntillCodeRuns()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment