Skip to content

Instantly share code, notes, and snippets.

@MateuszDabrowski
Last active June 28, 2020 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MateuszDabrowski/f433cecf3c02c400c9e74d08b78ad50c to your computer and use it in GitHub Desktop.
Save MateuszDabrowski/f433cecf3c02c400c9e74d08b78ad50c to your computer and use it in GitHub Desktop.
Script for inline day countdown counter
<p id="counter1">Only <span class="days"></span> days left</p>
<script>
function getTimeRemaining(endtime) {
const now = new Date().getTime();
const t = endtime - now;
const days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
};
}
function initializeClock(element, endtime) {
const clock = document.querySelector(element);
const daysSpan = clock.querySelector('.days');
function updateClock() {
const t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
const timeinterval = setInterval(updateClock, 1000);
}
/* Declare deadline time */
const deadline = new Date("2019-01-31").getTime()
/* Trigger counter function by targeting specific element and passing the above deadline */
initializeClock('#counter1', deadline);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment