How to restart countdown timer and why is it skipping seconds

How can I make it so the timer restarts itself when it hits zero?
Also, I have noticed sometimes it skips so instead of counting 5,4,3,2,1 it would go 5,4,3 1 – so misses out the two – How can I fix this?

const startingMinutes = 1;
let time = startingMinutes * 60;

const countdownEl = document.getElementById('countdown');

setInterval(updateCountdown, 1000);

function updateCountdown() {
    const minutes = Math.floor(time / 60);
    let seconds = time % 60;

    seconds = seconds < 10 ? '0' + seconds : seconds;

    countdownEl.innerHTML = `${minutes}: ${seconds}`;
    time--;
    time = time < 0 ? 0 : time; 

}

HTML

<p id="countdown"></p>

I look forward to your ideas.
Many thanks

122 thoughts on “How to restart countdown timer and why is it skipping seconds”

Leave a Comment