Anki/ts/editable/cooldown-timer.ts
llama 11c3e60615
Debounce mathjax rendering via cooldown instead (#4173)
* add cooldown timer

* debounce mathjax rendering via cooldown instead
2025-07-08 00:56:13 +07:00

31 lines
825 B
TypeScript

// 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);
}
}
}