Anki/ts/editable/change-timer.ts
llama 8ec139f62a
Debounce mathjax rendering to avoid stalling (#3827)
* move change-timer to editable

* debounce mathjax rendering
2025-02-21 16:39:11 +07:00

33 lines
811 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export class ChangeTimer {
private value: number | null = null;
private action: (() => void) | null = null;
constructor() {
this.fireImmediately = this.fireImmediately.bind(this);
}
schedule(action: () => void, delay: number): void {
this.clear();
this.action = action;
this.value = setTimeout(this.fireImmediately, delay) as any;
}
clear(): void {
if (this.value) {
clearTimeout(this.value);
this.value = null;
}
}
fireImmediately(): void {
if (this.action) {
this.action();
this.action = null;
}
this.clear();
}
}