Created
November 30, 2018 16:15
-
-
Save jefBinomed/021f61c771b8cc78c576f457e278bede to your computer and use it in GitHub Desktop.
2018-countdown-timer-es6
This file contains 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
'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