I'm working on a countdown timer for a quiz app I'm building as a homework assignment during bootcamp. Right now everything is functioning properly when rendered except that once the setInterval function inside of startQuiz() hits 0 it's supposed to clear the interval. However, the innerHTML written to the document keeps flashing momentarily as if the countdown is still running. I think it's probably a return in the wrong place but I've tried adding, subtracting, and moving returns throughout the script and am not seeing where the solution is.
var startButton = document.querySelector("#startButton");
var time = document.querySelector("#time");
var countdown = document.querySelector("#countdown");
var timeLeft;
var userScore;
startButton.addEventListener("click", startTimer);
function startTimer() {
var startCount = 5;
setInterval(function(){
if(startCount <= 0) {
clearInterval(startCount=0);
startQuiz();
}
countdown.innerHTML = startCount;
startCount -=1;
}, 1000);
return;
}
function startQuiz() {
timeLeft = 10;
quizQuestions();
setInterval(function(){
if(timeLeft <= 0) {
clearInterval(timeLeft=0);
gameOver();
}
time.innerHTML = timeLeft;
timeLeft -=1;
}, 1000)
return;
}
function gameOver() {
document.getElementById("quizQuestion").innerHTML = "GAME OVER";
return;
}
function quizQuestions() {
document.getElementById("quizQuestion").innerHTML = questionA.question;
return;
}
This is the entire script so far. I only left out the var definition for questionA since it is an object with multiple key value pairs and is not relevant to the issue so I omitted it to save space. The startTimer() function works properly as a separate countdown timer to start the game but I included it in the code in case it's interfering with the startQuiz() function in some way.