Skip to content

Instantly share code, notes, and snippets.

@Danny-G-Smith
Forked from Aaronkala/timer.js
Created July 22, 2018 16:39
Show Gist options
  • Save Danny-G-Smith/daea52b6eebd7855ec4ab7af5d631e1a to your computer and use it in GitHub Desktop.
Save Danny-G-Smith/daea52b6eebd7855ec4ab7af5d631e1a to your computer and use it in GitHub Desktop.
Countdown timer with Javascript
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
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.textContent = minutes + ":" + seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
function getTime() {
var fiveMinutes = 60 * 5,
display = document.querySelector('#time');
startTimer(fiveMinutes, display);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment