Table of Contents

Javascript Advance and Modules

other new ES6 codes

Useful code

array operation

UI interaction operation

Timer operation

let timer = 5 * 60; // Convert minutes to seconds
const timerIndicator = document.getElementById('timerIndicator');
 
// method a: Display count down, bad: will still run after time=0
setInterval(function() {
    timer--;
    timerIndicator.textContent = formatTimer(timer);
    if (timer <= 0) {
        alert('Form submit');
    }
}, 1000);
 
// method b: will not run after time=0
function updateTimer() {
    timer--;
    timerIndicator.textContent = formatTimer(timer);
    if (timer <= 0) {
        alert('Form submit');
    } else {
        setTimeout(updateTimer, 1000);
    }
}
updateTimer(); // Start the timer.
// end of method b
 
function formatTimer(seconds) {
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}

HTML5 and Javscript