Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for inline day countdown counter
<p id="counter">Only <span class="days"></span> days left</p>
<script>
function getTimeRemaining(endtime) {
var now = new Date().getTime();
var t = endtime - now;
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date("2019-01-31").getTime()
initializeClock('counter', deadline);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.