Created
January 22, 2019 01:27
-
-
Save Log1x/c9bffcaf6db6836c217d9d19e4ed8d38 to your computer and use it in GitHub Desktop.
Vanilla 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
/** | |
* Countdown | |
*/ | |
function init() { | |
const second = 1000, | |
minute = second * 60, | |
hour = minute * 60, | |
day = hour * 24; | |
const date = document.querySelector('[data-date]').dataset.date; | |
let countDown = new Date(date).getTime(), | |
x = setInterval(function () { | |
let now = new Date().getTime(), | |
distance = countDown - now; | |
if (document.querySelector('[data-days]') != null) { | |
document.querySelector('[data-days]').innerText = Math.floor(distance / (day)); | |
} | |
if (document.querySelector('[data-hours]') != null) { | |
document.querySelector('[data-hours]').innerText = Math.floor((distance % (day)) / (hour)); | |
} | |
if (document.querySelector('[data-minutes]') != null) { | |
document.querySelector('[data-minutes]').innerText = Math.floor((distance % (hour)) / (minute)); | |
} | |
if (document.querySelector('[data-seconds]') != null) { | |
document.querySelector('[data-seconds]').innerText = Math.floor((distance % (minute)) / second); | |
} | |
// if (distance < 0) { | |
// clearInterval(x); | |
// } | |
}, second) | |
} | |
export default { | |
init, | |
} |
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
<div class="countdown has-animation fadeInUp" data-date="Dec 31 2019 17:00:00"> | |
<div class="counter"> | |
<span class="value" data-days>0</span> | |
<span class="suffix">Days</span> | |
</div> | |
<div class="counter"> | |
<span class="value" data-hours>0</span> | |
<span class="suffix">Hours</span> | |
</div> | |
<div class="counter"> | |
<span class="value" data-minutes>0</span> | |
<span class="suffix">Mins</span> | |
</div> | |
<div class="counter"> | |
<span class="value" data-seconds>0</span> | |
<span class="suffix">Secs</span> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment