Last active
February 1, 2017 13:37
-
-
Save shisaq/1f62d8439771f494fc0730b327438042 to your computer and use it in GitHub Desktop.
a simple count down core code
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 = "Thu, Feb 22 2017 23:59:59 GMT-0500"; | |
// get the time remaining | |
function getTimeRemaining(endtime) { | |
var t = Date.parse(endtime) - Date.parse(new Date()); | |
var seconds = (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 | |
}; | |
} | |
// initialize clock | |
function initializeClock(id, endtime) { | |
var clock = document.getElementById(id); | |
var daysSpan = clock.querySelector('.days'); | |
var hoursSpan = clock.querySelector('.hours'); | |
var minutesSpan = clock.querySelector('.minutes'); | |
var secondsSpan = clock.querySelector('.seconds'); | |
function updateClock() { | |
var t = getTimeRemaining(endtime); | |
daysSpan.innerHTML = t.days; | |
hoursSpan.innerHTML = ('0' + t.hours).slice(-2); | |
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); | |
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); | |
if (t.total <= 0) { | |
clearInterval(timeinterval); | |
daysSpan.innerHTML = 0; | |
hoursSpan.innerHTML = 0; | |
minutesSpan.innerHTML = 0; | |
secondsSpan.innerHTML = 0; | |
} | |
} | |
updateClock(); | |
var timeinterval = setInterval(updateClock, 1000); | |
} | |
// var deadline = new Date(Date.parse(new Date()) + 97 * 24 * 60 * 60 * 1000); | |
initializeClock('clockdiv', deadline); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is not working in MAC safari....
The code i have used with my custom requirement
This Code works perfectly for mozilla and Crome...