Skip to content

Commit

Permalink
Add restart method
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Jan 27, 2021
1 parent 29fb69f commit c3ae9ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ This will add `Timeout` and `Interval` to `Date`.

Pauses the timer without reseting it. This is the main functionality of this class.

* `restart(): this`

Restarts the timer from zero, without firing any callback. Useful for autocomplete lists.

```ts
import { Timeout } from "date-timeout-interval";
const text = document.getElementById("myText");
const timeout = new Timeout(() => {
// do something...
}, 200);
text.oninput = () => {
timeout.restart();
};
```

* `await timeout`

The Timeout class implements the awaitable pattern, so you can await the timeout. It will throw an error if the timeout is stopped before finishing.
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ export class Timeout {
}

public stop(): this {
return this._stop(true);
}

private _stop(reject: boolean): this {
clearTimeout(this._timerId);
this._timerId = null;
this._timeLeft = 0;
this.state = 0;
this._rejecters.splice(0).forEach(r => r());
if (reject) this._rejecters.splice(0).forEach(r => r());
return this;
}

public restart(): this {
return this._stop(false).start();
}

public then(onResolve: () => void, onReject: () => void): void {
this._callbacks.push(onResolve);
this._rejecters.push(onReject);
Expand Down

0 comments on commit c3ae9ab

Please sign in to comment.