@panyam/tsutils
Version:
Some basic TS utils for personal use
45 lines • 1.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = void 0;
class Timer {
constructor(refreshInterval, stepFunc) {
this.refreshInterval = 1000;
this.lastRefreshAt = 0;
this.updateLoop = null;
this.refreshInterval = refreshInterval;
this.stepFunc = stepFunc;
}
stop() {
if (this.updateLoop != null) {
cancelAnimationFrame(this.updateLoop);
this.updateLoop = null;
}
}
start() {
this.kickOffUpdate();
}
kickOffUpdate() {
if (this.updateLoop != null) {
cancelAnimationFrame(this.updateLoop);
}
this.updateLoop = requestAnimationFrame((timestamp) => {
if (this.updateLoop != null) {
if (timestamp - this.lastRefreshAt >= this.refreshInterval) {
try {
this.stepFunc(timestamp);
}
catch (err) {
console.log("Error from Timer Handler: ", err);
alert("Error from Timer Handler: " + err.message);
return;
}
this.lastRefreshAt = timestamp;
}
this.updateLoop = null;
this.kickOffUpdate();
}
});
}
}
exports.Timer = Timer;
//# sourceMappingURL=timer.js.map