BrowserLogic/js/scheduler.js

61 lines
1.4 KiB
JavaScript

class Task {
constructor(taskname,taskdescription,tasktype,tasktime,callback,deleteonrun = false) {
// tasktype: 0: interval, 1: fixed time
this.Name = taskname;
this.Description = taskdescription;
this.Type = tasktype;
this.Enabled = true;
this.Time = tasktime;
this.LastCall = Date.now();
this.CallCount = 0;
this.DeleteOnRun = false;
if (deleteonrun) this.DeleteOnRun = true;
this.Callback = callback;
if (!(tasktype >= 0 && tasktype <= 1)) this.Type = 0;
}
CheckTime() {
let time = this.Time;
if (this.Type == 0) time = this.LastCall + this.Time;
if (this.Enabled && (Date.now() >= time)) {
this.LastCall = Date.now();
this.CallCount++;
if (this.Type == 1 || this.DeleteOnRun == true) this.Enabled = false;
this.Callback();
return true;
}
return false;
}
}
class ScheduleEngine {
constructor() {
this.Tasks = new Array();
}
addTask(task) {
this.Tasks.push(task);
}
deleteTask(task) {
for (let a = 0; a < this.Tasks.length; a++) {
if (this.Tasks[a] == task) {
this.Tasks.splice(a,1);
return true;
}
}
return false;
}
Tick() {
for (let a = 0; a < this.Tasks.length; a++) {
this.Tasks[a].CheckTime();
if (!this.Tasks[a].Enabled && this.Tasks[a].DeleteOnRun) {
this.Tasks.splice(a,1);
a--;
}
}
}
}