Skip to content

Instantly share code, notes, and snippets.

@Axighi
Created August 25, 2017 06:32
Show Gist options
  • Save Axighi/f27c60d2899bee652a37927853ca1038 to your computer and use it in GitHub Desktop.
Save Axighi/f27c60d2899bee652a37927853ca1038 to your computer and use it in GitHub Desktop.
export default class Timer {
constructor(time = 10, cb = () => console.log('null callback')) {
this.time = time
this.cb = cb
this.remain = time * 1000
}
start() {
this.startTime = new Date()
this.remain = this.time * 1000
clearTimeout(this.timeoutID)
this.timeoutID = setTimeout(() => {
this.cb()
}, this.remain)
}
pause() {
if (this.remain > 0) {
const now = new Date()
this.remain = now - this.startTime
clearTimeout(this.timeoutID)
}
}
resume() {
this.startTime = new Date()
this.timeoutID = setTimeout(() => {
this.remain = 0
this.cb()
}, this.remain)
}
restart() {
this.start();
}
clear() {
clearTimeout(this.timeoutID)
this.timeoutID = undefined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment