diff --git a/public/script.js b/public/script.js index efc99185c6290cadd41c33f13171ab8a1c3cd175..eab5ec9a0ff3f5a2cd70325f0e78dbe1b2d6494e 100644 --- a/public/script.js +++ b/public/script.js @@ -87,6 +87,27 @@ function displayQuestion(question) { btn.addEventListener('click', () => checkAnswer(answer, question.correctAnswer)); optionsContainer.appendChild(btn); }); + + startTimer(60); +} + +function startTimer(seconds) { + const timerElement = document.getElementById('timer'); + let timeLeft = seconds; + + const updateTimer = () => { + if (timeLeft > 0) { + timerElement.textContent = `Time left: ${timeLeft} seconds`; + timeLeft--; + } else { + timerElement.textContent = `Time's up!`; + socket.emit('nextQuestion', roomCode); + clearInterval(timerInterval); + } + }; + + updateTimer(); + const timerInterval = setInterval(updateTimer, 1000); } function checkAnswer(selectedAnswer, correctAnswer) { @@ -140,3 +161,4 @@ function updateUrlWithParams(params) { }); window.history.pushState({}, '', currentUrl); } +