Files
eewing ec317eb17c
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m11s
INIT
2026-02-18 15:17:47 -06:00

30 lines
715 B
JavaScript

import { onDestroyEffect } from "svelte-toolbelt";
export class TimeoutFn {
#interval;
#cb;
#timer = null;
constructor(cb, interval) {
this.#cb = cb;
this.#interval = interval;
this.stop = this.stop.bind(this);
this.start = this.start.bind(this);
onDestroyEffect(this.stop);
}
#clear() {
if (this.#timer !== null) {
window.clearTimeout(this.#timer);
this.#timer = null;
}
}
stop() {
this.#clear();
}
start(...args) {
this.#clear();
this.#timer = window.setTimeout(() => {
this.#timer = null;
this.#cb(...args);
}, this.#interval);
}
}