diff --git a/src/index.ts b/src/index.ts index 585754a..a9f6120 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,15 +9,14 @@ export class Timeout { private _startedAt: number; private _callback: () => void; private _timeLeft: number; - private _timerId: number; + private _timerId: any; // not possible in any other way public constructor(callback: () => void, timeMS: number, autoStart?: boolean) { - var self = this; - this._callback = function () { self.state = 3; callback(); } + this._callback = () => { this.state = 3; callback(); } this.currentTime = timeMS; this._startedAt = 0; this._timeLeft = timeMS; - this._timerId = -1; + this._timerId = null; this.state = 0; if (autoStart) this.start(); } @@ -27,15 +26,15 @@ export class Timeout { public start(timeMS?: number): this { if (this.state == 3) this.stop(); if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS; - if (this._timerId == -1) { + if (this._timerId == null) { if (this.state == 2) { this._startedAt = Date.now(); - this._timerId = setTimeout(this._callback, this._timeLeft); + this._timerId = setTimeout(this._callback, this._timeLeft); this.state = 1; } else if (this.state == 0) { this._startedAt = Date.now(); - this._timerId = setTimeout(this._callback, this.currentTime); + this._timerId = setTimeout(this._callback, this.currentTime); this.state = 1; } } @@ -44,16 +43,16 @@ export class Timeout { public pause(): this { if (this.state != 1) return this; - clearTimeout(this._timerId); - this._timerId = -1; + clearTimeout(this._timerId); + this._timerId = null; this._timeLeft -= Date.now() - this._startedAt; this.state = 2; return this; } public stop(): this { - clearTimeout(this._timerId); - this._timerId = -1; + clearTimeout(this._timerId); + this._timerId = null; this._timeLeft = 0; this.state = 0; return this; @@ -71,16 +70,15 @@ export class Interval { private _lastTrigger: number; private _callback: () => void; private _timeLeft: number; - private _timerId: number; + private _timerId: any; // not possible in any other way private _isInTimeout: boolean; public constructor(callback: (() => void), timeMS?: number, autoStart?: boolean) { - var self = this; this._callback = () => { this._lastTrigger = Date.now(); callback(); }; this.currentTime = timeMS; this._lastTrigger = 0; this._timeLeft = timeMS; - this._timerId = -1; + this._timerId = null; this.state = 0; this._isInTimeout = false; if (autoStart) this.start(); @@ -90,20 +88,19 @@ export class Interval { public start(timeMS: number): this; public start(timeMS?: number): this { if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS; - if (this._timerId == -1) { + if (this._timerId == null) { if (this.state == 2) { this._lastTrigger = Date.now(); - var self = this; - this._timerId = setTimeout(function () { + this._timerId = setTimeout(() => { this._isInTimeout = false; - this._timerId = setInterval(self._callback, self.currentTime); + this._timerId = setInterval(this._callback, this.currentTime); }, this._timeLeft); this.state = 1; this._isInTimeout = true; } else if (this.state == 0) { this._lastTrigger = Date.now(); - this._timerId = setInterval(this._callback, this.currentTime); + this._timerId = setInterval(this._callback, this.currentTime); this.state = 1; } } @@ -112,16 +109,16 @@ export class Interval { public pause(): this { if (this.state != 1) return this; - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); + this._timerId = null; this._timeLeft -= Date.now() - this._lastTrigger; this.state = 2; return this; } public stop(): this { - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); + this._timerId = null; this._timeLeft = 0; this.state = 0; this._isInTimeout = false;