JavaScript: Creating a Down Counter

Example: A counter that will count down from 20 in one second intervals.

javascript code snippets

JavaScript: Creating a Down Counter

Example: A counter that will count down from 20 in one second intervals

var counter = 20 // say we have a 20 seconds down counter

var interval = setInterval(() => {
    if (counter-- < 0) {
        clearInterval(interval);
    } else {
        if (counter >= 0) {
            // we get here every second while counting
        } else {
            // timer has finished
        }
    }
}, 1000); // 1000 is 1000mS or One Second