Skip to content

Instantly share code, notes, and snippets.

@estorgio
Created March 12, 2020 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estorgio/9867bc98af7b2ad3acb2c98f0aae9f13 to your computer and use it in GitHub Desktop.
Save estorgio/9867bc98af7b2ad3acb2c98f0aae9f13 to your computer and use it in GitHub Desktop.
Scheduling using setInterval
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