#f: () => any
#elapsed: number = 0
#isPaused: boolean = false
+ #remaining: number
#start: number
#ticker: number | NodeJS.Timeout
#timeout: number | NodeJS.Timeout
constructor (f: () => any, t: number) {
- this.#ticker = setInterval(Function.prototype, 1000)
this.#f = f
+ this.#remaining = t
this.#start = performance.now()
- this.#timeout = setTimeout(this.#f, t)
+ this.#ticker = setInterval(Function.prototype, 1000)
+ this.#timeout = setTimeout(this.#f, this.#remaining)
}
pause () {
if (!this.#isPaused) {
- clearInterval(this.#ticker)
clearTimeout(this.#timeout)
- this.#elapsed = performance.now() - this.#start
+ clearInterval(this.#ticker)
+ const elapsed = performance.now() - this.#start
+ this.#remaining -= elapsed
this.#isPaused = true
}
}
resume () {
if (this.#isPaused) {
- this.#ticker = setInterval(Function.prototype, 1000)
this.#start = performance.now()
- this.#timeout = setTimeout(this.#f, this.#elapsed)
+ this.#ticker = setInterval(Function.prototype, 1000)
+ this.#timeout = setTimeout(this.#f, this.#remaining)
this.#isPaused = false
}
}