anki/ts/editor/change-timer.ts
Henrik Giesel c30ba6a3f6 Add ChangeTimer.prototype.fireImmediately
so Mathjax is saved when exiting editor prematurely
2021-09-16 14:47:05 +02:00

34 lines
804 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);
}
clear(): void {
if (this.value) {
clearTimeout(this.value);
this.value = null;
}
}
fireImmediately(): void {
if (this.action) {
this.action();
this.action = null;
}
this.clear();
}
}