Hi everyone
I created a working countdown but I want to transform my code in OOP. When I click on the “run” button, everything is correct but the countdown doesn’t decrease automatically every seconds.
You can see the past working countdown code with no OOP.
Could you guys take a look? Thanks
class Timer {
constructor() {
this.counter = document.getElementById("counter");
this.button = document.getElementById("button");
this.buttonRemove = document.getElementById("remove");
this.secondes = 13;
this.minutes = 0;
this.current_secs;
this.current_mins;
this.buttonClick();
this.removeClick();
this.ifstorage();
}
ifstorage() {
if (sessionStorage.getItem("Secondes")) {
this.secondes = sessionStorage.getItem("Secondes");
this.minutes = sessionStorage.getItem("Minutes");
this.running();
}
}
removeStorage() {
sessionStorage.clear();
}
storage() {
sessionStorage.setItem("Secondes", this.current_secs);
sessionStorage.setItem("Minutes", this.current_mins);
}
buttonClick() {
this.button.addEventListener("click", () => {
// secondesCounting();
this.running();
});
}
removeClick() {
this.buttonRemove.addEventListener("click", () => {
this.removeStorage();
});
}
secondesCounting() {
if (this.secondes > 0) {
this.secondes--;
this.current_secs = this.secondes;
this.current_mins = this.minutes;
this.addTextContent()
this.storage();
} else if (this.secondes == 0 && this.minutes > 0) {
this.secondes = 59;
this.minutes--;
this.current_secs = this.secondes;
this.current_mins = this.minutes;
this.addTextContent();
this.storage();
} else if (this.secondes == 0 && this.minutes == 0) {
clearInterval();
sessionStorage.clear();
this.counter.textContent = "fin";
}
}
running() {
this.secondesCounting();
setInterval(this.secondesCounting, 1000);
}
addTextContent() {
this.counter.textContent = this.current_mins + ":" + this.current_secs;
}
}
let countdown = new Timer();
<button id="button">Run</button><br><br>
<div id="counter"></div><br><br>
<button id="remove">Remove</button>