Created
March 12, 2020 10:25
-
-
Save estorgio/9867bc98af7b2ad3acb2c98f0aae9f13 to your computer and use it in GitHub Desktop.
Scheduling using setInterval
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
let lastRun = 0; | |
function runTask() { | |
// Insert code to run on schedule | |
console.log('Your task has been executed!'); | |
} | |
function checkSchedule() { | |
const hour = new Date().getHours(); | |
const minute = new Date().getMinutes(); | |
// Schedule task at 6:30 AM in the morning | |
if (hour === 6 && minute === 30) { | |
// Check if ran less than a minute ago | |
const msSinceLastRun = Date.now() - lastRun; | |
if (msSinceLastRun < 60000) return; | |
// Execute task | |
runTask(); | |
// Record the timestamp | |
lastRun = Date.now(); | |
} | |
} | |
setInterval(checkSchedule, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment