Created
August 25, 2017 06:32
-
-
Save Axighi/f27c60d2899bee652a37927853ca1038 to your computer and use it in GitHub Desktop.
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
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