Skip to content

Instantly share code, notes, and snippets.

@jefBinomed
Created November 30, 2018 16:15
Show Gist options
  • Save jefBinomed/021f61c771b8cc78c576f457e278bede to your computer and use it in GitHub Desktop.
Save jefBinomed/021f61c771b8cc78c576f457e278bede to your computer and use it in GitHub Desktop.
2018-countdown-timer-es6
'use strict';
export default class Timer {
constructor(callback){
// Target Time : '2018-10-18T09:00:00'
this.targetDate = new Date(Date.now() + 30 * 1000 + 120 * 1000);
this.callback = callback;
this.checkTime();
}
checkTime() {
const now = Date.now();
if (now > this.targetDate.getTime()) {
this.callback({
type: 'endCountDown',
value: true,
});
return;
}
let diff = this.targetDate.getTime() - now;
const minutes = new Intl.NumberFormat('fr', {
minimumIntegerDigits: 2,
useGrouping: false,
}).format(Math.floor(diff / (60 * 1000)));
const seconds = new Intl.NumberFormat('fr', {
minimumIntegerDigits: 2,
useGrouping: false,
}).format(Math.floor((diff % (60 * 1000)) / 1000));
const lastMinute = diff < 60 * 1000;
this.callback({
type: 'time',
value: {
minutes,
seconds,
lastMinute,
diff,
},
});
window.requestAnimationFrame(this.checkTime.bind(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment