Skip to content

Instantly share code, notes, and snippets.

@giansalex
Last active June 7, 2018 15:47
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 giansalex/390726462799c26929dad2f360ac3f36 to your computer and use it in GitHub Desktop.
Save giansalex/390726462799c26929dad2f360ac3f36 to your computer and use it in GitHub Desktop.
CounterDown - Javascript
function format2(value) {
if (value < 10) return '0' + value;
return value;
}
// Set the date we're counting down to
var countDownDate = 3*60000;
var now = 0;
var element = document.getElementById("demo");
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
// Find the distance between now an the count down date
var distance = countDownDate - now;
now += 1000;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
element.innerHTML = format2(minutes) + ":" + format2(seconds);
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
element.innerHTML = "EXPIRED";
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment