Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Jan 18, 2021
1 parent 33d973c commit 3a2e47c
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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 = <any>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 = <any>setTimeout(this._callback, this.currentTime);
this._timerId = setTimeout(this._callback, this.currentTime);
this.state = 1;
}
}
Expand All @@ -44,16 +43,16 @@ export class Timeout {

public pause(): this {
if (this.state != 1) return this;
clearTimeout(<any>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(<any>this._timerId);
this._timerId = -1;
clearTimeout(this._timerId);
this._timerId = null;
this._timeLeft = 0;
this.state = 0;
return this;
Expand All @@ -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();
Expand All @@ -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 = <any>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 = <any>setInterval(this._callback, this.currentTime);
this._timerId = setInterval(this._callback, this.currentTime);
this.state = 1;
}
}
Expand All @@ -112,16 +109,16 @@ export class Interval {

public pause(): this {
if (this.state != 1) return this;
this._isInTimeout ? clearTimeout(<any>this._timerId) : clearInterval(<any>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(<any>this._timerId) : clearInterval(<any>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;
Expand Down

0 comments on commit 3a2e47c

Please sign in to comment.