Last active
December 20, 2015 11:29
-
-
Save mapsam/6123346 to your computer and use it in GitHub Desktop.
JS Countdown Timer
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
<!DOCTYPE html> | |
<head> | |
<style> | |
body { | |
text-align:center; | |
} | |
#clock { | |
font-family:arial; | |
font-size:2em; | |
font-weight:bold; | |
color:#555; | |
</style> | |
</head> | |
<body> | |
<p id="clock"></p> | |
<script> | |
var end = new Date('08/19/2013 10:1 AM'), | |
sec = 1000, | |
min = sec * 60, | |
hour = min * 60, | |
day = hour * 24, | |
timer; | |
function remain() { | |
var now = new Date(), | |
between = end - now; | |
var days = Math.floor(between / day), | |
hours = Math.floor((between % day) / hour), | |
minutes = Math.floor((between % hour) / min), | |
seconds = Math.floor((between % min) / sec); | |
var dayString = 'days ', | |
hourString = 'hrs ', | |
minString = 'mins ', | |
secString = 'secs '; | |
if (days == 1) { | |
dayString = 'day '; | |
}; | |
if (hours == 1) { | |
hourString = 'hr '; | |
}; | |
if (minutes == 1) { | |
minString = 'min '; | |
}; | |
if (seconds == 1) { | |
secString = 'sec '; | |
}; | |
var clock = days + dayString + hours + hourString + minutes + minString + seconds + secString; | |
document.getElementById("clock").innerHTML = clock; | |
} | |
timer = setInterval(function () { | |
remain(); | |
}, 1000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment