Firt commit of timer module
This commit is contained in:
@@ -4,5 +4,6 @@ console.log('Load utils')
|
||||
module.exports = {
|
||||
get equipment() { return require('./equipment') },
|
||||
get lib() { return require('./lib') },
|
||||
get timer() { return require('./timer')},
|
||||
get watch() { return require('./watch') },
|
||||
}
|
||||
54
utils/timer.js
Normal file
54
utils/timer.js
Normal file
@@ -0,0 +1,54 @@
|
||||
console.loggerName = 'js.timer';
|
||||
console.log('Load timer module');
|
||||
|
||||
class Timer {
|
||||
|
||||
#timers = new Object();
|
||||
|
||||
constructor() {
|
||||
console.log('Initialization of timer');
|
||||
}
|
||||
|
||||
create(identifier, timeout, func) {
|
||||
console.debug(`Create timer with identifier ${identifier}`);
|
||||
this.#timers[identifier] = actions.ScriptExecution.createTimer(identifier, timeout, func);
|
||||
}
|
||||
|
||||
cancel(identifier) {
|
||||
if (!this.#timers.hasOwnProperty(identifier)) {
|
||||
console.debug(`No timer with identifier ${identifier} available to cancel`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.#timers[identifier].isActive()) {
|
||||
console.debug(`Timer with identifier ${identifier} not running. Cancel anyway`);
|
||||
} else {
|
||||
console.debug(`Cancel timer with identifier ${identifier}`);
|
||||
}
|
||||
|
||||
this.#timers[identifier].cancel();
|
||||
delete this.#timers[identifier];
|
||||
}
|
||||
|
||||
cancelAll() {
|
||||
|
||||
let timers = Object.keys(this.#timers);
|
||||
|
||||
if (timers.length == 0) {
|
||||
console.debug('No timers available to cancel');
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let timer of timers) {
|
||||
this.cancel(timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
Timer,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user