You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/service/cron.js

34 lines
829 B
JavaScript

class Cron {
constructor() {
this.tasks = [];
}
plan(time, task) {
const now = new Date();
if (!(time instanceof Date)) time = new Date(time);
this.tasks.push({
id: setTimeout(() => {
for (const i in this.tasks) {
if (this.tasks[i].func === task) {
this.tasks.splice(i, 1);
break;
}
}
task();
}, time - now),
func: task,
});
}
interval(time, task) {
const now = new Date();
if (!(time instanceof Date)) time = new Date(time);
this.tasks.push({
id: setTimeout(() => { task(); }, time - now),
func: task,
});
}
}
module.exports = Cron;