|
@@ -0,0 +1,25 @@
|
|
|
+/**
|
|
|
+ * Creates a function which debounces with the given timeout and resets the timer
|
|
|
+ * on every function call.
|
|
|
+ *
|
|
|
+ * The returned debounced function has a function property abort() which aborts the timer.
|
|
|
+ *
|
|
|
+ * @memberOf function
|
|
|
+ * @function
|
|
|
+ * @param {function} fn
|
|
|
+ * @param {number} timeout
|
|
|
+ * @return {function(...[*])}
|
|
|
+ */
|
|
|
+export default (fn, timeout = 0) => {
|
|
|
+ let timer = null;
|
|
|
+ const abortTimer = () => clearTimeout(timer);
|
|
|
+
|
|
|
+ const debouncedFunction = (...args) => {
|
|
|
+ abortTimer();
|
|
|
+ timer = setTimeout(() => fn(...args), timeout)
|
|
|
+ };
|
|
|
+
|
|
|
+ debouncedFunction.abort = abortTimer;
|
|
|
+
|
|
|
+ return debouncedFunction;
|
|
|
+};
|