Last active
June 7, 2018 15:47
-
-
Save giansalex/390726462799c26929dad2f360ac3f36 to your computer and use it in GitHub Desktop.
CounterDown - Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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