better-timer
Version:
A promise-based timer that can be paused
3 lines (2 loc) • 753 B
JavaScript
(()=>{var e=class{constructor(t,...i){this.duration=t;this.callbacks=[];this.isRunning=!1;this.callbacks.push(...i),this.startedAt=this.currentTimestamp,this.timeLeft=this.duration,this.createTimer(this.duration)}execCallbacks(){this.callbacks.forEach(t=>{t()})}get promise(){return new Promise(t=>{this.callbacks.push(t)})}createTimer(t){this.isRunning||(this.timer=setTimeout(()=>{this.execCallbacks(),this.isRunning=!1},t),this.isRunning=!0)}pause(){this.cancel();let t=this.currentTimestamp-this.startedAt,i=this.duration-t;this.timeLeft=i}resume(){this.createTimer(this.timeLeft)}cancel(){clearTimeout(this.timer),this.isRunning=!1}get currentTimestamp(){return window.performance.now()}};var n=e;})();
//# sourceMappingURL=better-timer.min.js.map