Skip to content

Commit

Permalink
.start takes an optional parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Jan 14, 2021
1 parent 06443f5 commit f300b61
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f300b61

Please sign in to comment.