Skip to content

Instantly share code, notes, and snippets.

@19h47
Created November 6, 2017 09:53
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 19h47/bc0fb6dc1ef229c6cc142a76049d5a88 to your computer and use it in GitHub Desktop.
Save 19h47/bc0fb6dc1ef229c6cc142a76049d5a88 to your computer and use it in GitHub Desktop.
Countdown
/**
* Countdown
*
* @see https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_countdown
*/
class Countdown {
/**
* Countdown.constructor
*/
constructor() {
console.info('Countdown.constructor');
// Set the date we're counting down to
this.countDownDate = new Date('Dec 2, 2017 14:00:00').getTime();
this.$element = document.getElementById('countdown');
this.days = null;
this.hours = null;
this.minutes = null;
this.secondes = null;
this.now = null;
this.distance = null;
this.interval = setInterval(() => {
this.setup();
}, 1000);
}
setDays() {
this.days = Math.floor(this.distance / (1000 * 60 * 60 * 24));
this.$element.querySelector('.js-days').innerHTML = this.days;
}
setHours() {
this.hours = Math.floor((this.distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
this.$element.querySelector('.js-hours').innerHTML = this.hours;
}
setMinutes() {
this.minutes = Math.floor((this.distance % (1000 * 60 * 60)) / (1000 * 60));
this.$element.querySelector('.js-minutes').innerHTML = this.minutes;
}
setSecondes() {
this.secondes = Math.floor((this.distance % (1000 * 60)) / 1000);
this.$element.querySelector('.js-secondes').innerHTML = this.secondes;
}
setup() {
// Get todays date and time
this.now = new Date().getTime();
// Find the distance between now an the count down date
this.distance = this.countDownDate - this.now;
this.setDays();
this.setHours();
this.setMinutes();
this.setSecondes();
if (this.distance < 0) {
clearInterval(this.interval);
this.$element.innerHTML = 'EXPIRED';
}
}
}
export default Countdown;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment