about summary refs log tree commit diff
path: root/client/src/lib/utils/debounce.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/lib/utils/debounce.ts')
-rw-r--r--client/src/lib/utils/debounce.ts12
1 files changed, 12 insertions, 0 deletions
diff --git a/client/src/lib/utils/debounce.ts b/client/src/lib/utils/debounce.ts
new file mode 100644
index 0000000..86ef3db
--- /dev/null
+++ b/client/src/lib/utils/debounce.ts
@@ -0,0 +1,12 @@
+type Procedure = (...args: any[]) => any;
+
+export default function <F extends Procedure>(f: F, duration: number) {
+    let timeout: ReturnType<typeof setTimeout> | null = null;
+    return function (...args: Parameters<F>) {
+        if (timeout !== null) {
+            clearTimeout(timeout);
+            timeout = null;
+        }
+        timeout = setTimeout(() => f(...args), duration);
+    };
+}