From f300b61ec45df18b2be81a2090b08b2de81f31b2 Mon Sep 17 00:00:00 2001 From: blaumeise20 <62756994+blaumeise20@users.noreply.github.com> Date: Thu, 14 Jan 2021 18:59:51 +0100 Subject: [PATCH] .start takes an optional parameter --- README.md | 10 +++++++++- index.d.ts | 2 ++ index.js | 10 +++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7e770c7..fc2ecb8 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,11 @@ This will add `Timeout` and `Interval` to `Date`. * `start(): this` - Starts the timer. If the timer is currently paused, it will resume the timer. + Starts the timer. If the timer is currently paused, it will resume the timer. If the timer is already done, it will reset it and start as normal. + +* `start(timeMS: number): this` + + Starts the timer with the given time. **Only works, if the timer is not running or paused!** * `stop(): this` @@ -72,6 +76,10 @@ This will add `Timeout` and `Interval` to `Date`. Starts the interval. If the interval is currently paused, it will resume the interval. +* `start(timeMS: number): this` + + Starts the interval with the given time. **Only works, when the interval is reset!** + * `stop(): this` Stops the interval and resets it to 0. Acts like `clearInterval`. diff --git a/index.d.ts b/index.d.ts index 5f4c2bc..dbbc5f2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,7 @@ export declare class Timeout { public constructor(callback: () => void, timeMS: number, autoStart: boolean); public start(): this; + public start(timeMS: number): this; public stop(): this; public pause(): this; public readonly state: 0 | 1 | 2 | 3; @@ -10,6 +11,7 @@ export declare class Timeout { export declare class Interval { public constructor(callback: () => void, timeMS: number, autoStart: boolean); public start(): this; + public start(timeMS: number): this; public stop(): this; public pause(): this; public readonly state: 0 | 1 | 2; diff --git a/index.js b/index.js index f9e4320..abd6efe 100644 --- a/index.js +++ b/index.js @@ -8,10 +8,9 @@ function Timeout(callback, time, autoStart = false) { this.state = 0; if (autoStart) this.start(); }; -Timeout.prototype.start = function start(time) { - if (arguments.length > 0) { // warning: undocumented feature, not really working - this.currentTime = time; - } +Timeout.prototype.start = function start(timeMS) { + if (this.state == 3) this.stop(); + if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS; if (this._timerId == -1) { if (this.state == 2) { this._startedAt = Date.now(); @@ -59,7 +58,8 @@ function Interval(callback, time, autoStart = false) { this._isInTimeout = false; if (autoStart) this.start(); }; -Interval.prototype.start = function start() { +Interval.prototype.start = function start(timeMS) { + if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS; if (this._timerId == -1) { if (this.state == 2) { this._lastTrigger = Date.now();