Attempting to pause a timer I have set for a quiz for my class. I can get the button to pause the time, but pressing play to resume does nothing. I am fairly new to JavaScript and have been spinning my wheels on this for awhile.
Ive tried to incorporate the isPaused boolean in attempt to fix my broken code, to no avail.
//Timer
var secondsElapsed = 0;
var totalSeconds = 0;
var interval;
var minutesDisplay = document.querySelector("#minutes");
var secondsDisplay = document.querySelector("#seconds");
var playButton = document.querySelector("#play");
var pauseButton = document.querySelector("#pause");
var isPaused = false;
function runClockCb() {
secondsElapsed++
minutesDisplay.textContent = Math.floor((totalSeconds - secondsElapsed) / 60);
secondsDisplay.textContent = (totalSeconds - secondsElapsed) % 60;
if (secondsElapsed >= totalSeconds && !isPaused) {
clearInterval(interval);
}
}
function startTimer() {
var minutes = 3
totalSeconds = minutes * 60
secondsElapsed = 0;
if (typeof interval !== "undefined") {
clearInterval(interval);
startTimer.preventDefault();
isPaused = false;
}
interval = setInterval(runClockCb, 1000);
}
function pauseTimer(){
clearInterval(interval);
startTimer.preventDefault();`enter code here`
isPaused = true
}
playButton.addEventListener("click", startTimer);
pauseButton.addEventListener("click", pauseTimer)