Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rtviner
Created June 1, 2019 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtviner/34478fcea668c4b83bff77a1806ee302 to your computer and use it in GitHub Desktop.
Save rtviner/34478fcea668c4b83bff77a1806ee302 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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