I’m looking to make a 30-minute countdown timer that I can stop/start/reset. I’ve looked up a bunch of JS code, and this one makes the most sense to me, but it still doesn’t work. Any ideas?
const defaultValue = 30 * 60;
var countDownTime = defaultValue;
const findTimeString = () => {
var minutes = String(Math.trunc(countDownTime / 60));
var seconds = String(countDownTime % 60);
if (minutes.length === 1) {
minutes = “0” + minutes;
}
if (seconds.length === 1) {
seconds = “0” + seconds;
}
return minutes + seconds;
};
const countContainer = document.querySelectorAll(“.count-digit”);
const renderTime = () => {
const time = findTimeString();
countContainer.forEach((count, index) => {
count.innerHTML = time.charAt(index);
});
};
const startAction = document.getElementById(“start-timer”);
const startTimer = () => {
if (isStopped) {
isStopped = false;
timerID = setInterval(runCountDown, 500);
}
};
startAction.onclick = startTimer;
const runCountDown = () => {
countDownTime -= 1;
renderTime();
if (countDownTime === 0) {
stopTimer();
countDownTime = defaultValue;
}
};
const stopAction = document.getElementById(“stop-timer”);
const stopTimer = () => {
isStopped = true;
if (timerID) {
clearInterval(timerID);
}
};
stopAction.onclick = stopTimer;
const resetAction = document.getElementById(“reset-timer”);
const resetTimer = () => {
stopTimer();
countDownTime = defaultValue;
renderTime();
};
resetAction.onclick = resetTimer;