Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
countdown timer
function countdown (minutes) {
for (let seconds = minutes * 60 ; seconds >= 0; seconds--) {
setTimeout(() => tick(seconds), 1000);
}
function tick (seconds) {
let secondsView = ((seconds%60) >= 10) ? seconds%60 : `0${seconds%60}`;
console.log(`${Math.floor(seconds/60)||"00"}:${secondsView||"00"}`);
}
}
@rtviner

This comment has been minimized.

Copy link
Owner Author

@rtviner rtviner commented Jun 1, 2019

function countdown (minutes) {

for (let seconds = minutes * 60 ; seconds >= 0; seconds--) {
    tick(seconds);
}

function tick (seconds) {
    setTimeout(function() {
        let secondsView = ((seconds%60) >= 10) ? seconds%60 : `0${seconds%60}`;
        console.log(`${Math.floor(seconds/60)||"00"}:${secondsView||"00"}`);
    }, 1000 * (minutes * 60 - seconds));    
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment