Countdown timer

Andrijan Portrait

Andrijan Tasevski ยท 03 Nov, 2022

function initTimer(seconds) {
    let time = seconds;

    timerID = setInterval(updateTimer, 1000);

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

        const countdown = `${minutes < 10 ? "0" + minutes : minutes}:${seconds < 10 ? "0" + seconds : seconds}`;

        if (time === 0) {
            clearInterval(timerID);
        }

        console.log(countdown);

        time--;
    }
}

initTimer(180);