@tbowmo/node-red-small-timer
Version:
Small timer node for Node-RED with support for sunrise, sunset etc. timers
35 lines (34 loc) • 896 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
const granularity = 60000;
class Timer {
constructor() {
this.currentTimeout = 0;
}
start(desiredTimeout, cb) {
const timeout = desiredTimeout * granularity;
this.currentTimeout = Date.now() + timeout;
if (cb) {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(cb, timeout);
}
}
stop() {
this.currentTimeout = 0;
if (this.timer) {
clearTimeout(this.timer);
this.timer = undefined;
}
}
active() {
return Date.now() < this.currentTimeout;
}
timeLeft() {
const time = this.currentTimeout - Date.now();
return time > 0 ? (time / granularity) : 0;
}
}
exports.Timer = Timer;