Skip to content

Instantly share code, notes, and snippets.

@genesisneo
Created May 2, 2016 05:41
Show Gist options
  • Save genesisneo/631423fbba9dd75837e13233e8b0ec71 to your computer and use it in GitHub Desktop.
Save genesisneo/631423fbba9dd75837e13233e8b0ec71 to your computer and use it in GitHub Desktop.
JavaScript | Countdown Timer
// Countdown timer script, after 25secs it will add class on the body class="stop-animations"
// and clear the intervar. Timer value (60 * 1 = 1 minute)
function startTimer(duration, display, isRunning) {
if (!!isRunning) {
var timer = duration, minutes, seconds;
var interval = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.innerHTML = minutes + ":" + seconds;
if (--timer < 0) {
timer = 0;
document.getElementsByTagName('body')[0].className+=' stop-animations';
clearInterval(interval);
}
}, 1000);
}
}
var time = 25, display = document.getElementById("count");
startTimer(time, display, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment