mirror of
https://github.com/ankitects/anki.git
synced 2026-01-13 14:03:55 -05:00
add cooldown timer
This commit is contained in:
parent
a83a6b5928
commit
64691a812e
1 changed files with 31 additions and 0 deletions
31
ts/editable/cooldown-timer.ts
Normal file
31
ts/editable/cooldown-timer.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
export class CooldownTimer {
|
||||
private executing = false;
|
||||
private queuedAction: (() => void) | null = null;
|
||||
private delay: number;
|
||||
|
||||
constructor(delayMs: number) {
|
||||
this.delay = delayMs;
|
||||
}
|
||||
|
||||
schedule(action: () => void): void {
|
||||
if (this.executing) {
|
||||
this.queuedAction = action;
|
||||
} else {
|
||||
this.executing = true;
|
||||
action();
|
||||
setTimeout(this.#pop.bind(this), this.delay);
|
||||
}
|
||||
}
|
||||
|
||||
#pop(): void {
|
||||
this.executing = false;
|
||||
if (this.queuedAction) {
|
||||
const action = this.queuedAction;
|
||||
this.queuedAction = null;
|
||||
this.schedule(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue