var timeLeft = 60;
const elem = document.getElementById('timer');
countdown(); // To prevent the timer from starting after a 1 second pause
const timerId = setInterval(countdown, 1000);
function countdown() {
if (timeLeft == -1) {
clearTimeout(timerId); // To stop the timer
} else {
elem.innerHTML = timeLeft;
timeLeft--;
}
seconds = 60 - timeLeft; // The time taken to complete the quiz
if (timeLeft == -1) { // If the timer runs out, all elements will disappear, and ONLY the score will be displayed
outline.style.display = 'none';
square.style.display = 'none';
title.style.display = 'none';
quizTimer.style.display = 'none';
displayResult.style.display = '';
displayResult.textContent = 'Score = ' + score;
}
}
Пожалуйста, добавьте фрагмент кода вместо изображения.
его там в ссылке "таймер js код"
о, ладно, извините, я новичок в этом
Он уменьшен в timeLeft--. Следующий вопрос: как он выполняется раз в секунду? setInterval



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Если вы спрашиваете:
Where does the
timeLeftvariable reduce
И вы имеете в виду:
Where in the code does the
timeLeftvariable's value decrease
Тогда я могу сказать, что это происходит здесь, когда вы пост-декрементируете значение (по сути, это означает вычитание единицы):
else {
elem.innerHTML = timeLeft;
timeLeft--;
}
ок, спасибо, еще один вопрос, почему "if (timeLeft == -1)", а не "if (timeLeft == 0)"
Я не знаю @AbdullahAjmal, контекста кода недостаточно.
@AbdullahAjmal timeLeft == -1, потому что при timeLeft == 0 минута все еще очень жива
Где код?