Auto updating timer. Courtesy of: http://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/ I'm dissecting this one to write my own variation.
Created
March 2, 2016 14:20
-
-
Save chaotic-stump/ca61e0d65ca33051c35c to your computer and use it in GitHub Desktop.
succinct countdown
This file contains 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
<div id="clockdiv"> | |
Days: <span class="days"></span><br> | |
Hours: <span class="hours"></span><br> | |
Minutes: <span class="minutes"></span><br> | |
Seconds: <span class="seconds"></span> | |
</div> |
This file contains 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
var deadline = 'June 11 2016 13:00:00'; | |
function getTimeRemaining(endtime){ | |
var t = Date.parse(endtime) - Date.parse(new Date()); | |
var seconds = Math.floor( (t/1000) % 60 ); | |
var minutes = Math.floor( (t/1000/60) % 60 ); | |
var hours = Math.floor( (t/(1000*60*60)) % 24 ); | |
var days = Math.floor( t/(1000*60*60*24) ); | |
return { | |
'total': t, | |
'days': days, | |
'hours': hours, | |
'minutes': minutes, | |
'seconds': seconds | |
}; | |
} | |
function initializeClock(id, endtime){ | |
var clock = document.getElementById(id); | |
function updateClock(){ | |
var t = getTimeRemaining(endtime); | |
clock.innerHTML = 'days: ' + t.days + '<br>' + | |
'hours: '+ t.hours + '<br>' + | |
'minutes: ' + t.minutes + '<br>' + | |
'seconds: ' + t.seconds; | |
if(t.total<=0){ | |
clearInterval(timeinterval); | |
} | |
} | |
updateClock(); // run function once at first to avoid delay | |
var timeinterval = setInterval(updateClock,1000); | |
} | |
initializeClock('clockdiv', deadline); | |
var daysSpan = clock.querySelector('.days'); | |
var hoursSpan = clock.querySelector('.hours'); | |
var minutesSpan = clock.querySelector('.minutes'); | |
var secondsSpan = clock.querySelector('.seconds'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment