@panyam/tsutils
Version:
Some basic TS utils for personal use
41 lines • 1.32 kB
JavaScript
export 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();
}
});
}
}
//# sourceMappingURL=timer.js.map