Files
2025-10-09 12:56:32 +02:00

38 lines
763 B
JavaScript

class Countdown {
constructor(duration, onTick, onFinish) {
this.duration = duration;
this.timeLeft = duration;
this.onTick = onTick;
this.onFinish = onFinish;
this.timer = null;
}
start() {
this.stop();
this.timeLeft = this.duration;
this.onTick?.(this.timeLeft);
this.timer = setInterval(() => {
this.timeLeft--;
this.onTick?.(this.timeLeft);
if(this.timeLeft <= 0) {
this.stop();
this.onFinish();
}
}, 1000);
}
stop() {
if(this.timer) {
clearInterval(this.timer);
}
this.timer = null;
}
reset() {
this.start();
}
}