From 54edd4b5178eb306fd4f2da6e2be2165e56e1f8d Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 2 Sep 2021 19:49:57 +0200 Subject: [PATCH] Implement debounce function --- resources/js/helpers.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 resources/js/helpers.js diff --git a/resources/js/helpers.js b/resources/js/helpers.js new file mode 100644 index 0000000..9ab35e7 --- /dev/null +++ b/resources/js/helpers.js @@ -0,0 +1,22 @@ +export function debounce(func, wait, immediate) { + let timeout; + return function() { + let context = this, args = arguments; + + let later = function() { + timeout = null; + if (!immediate) { + func.apply(context, args); + } + }; + + let callNow = immediate && !timeout; + + clearTimeout(timeout); + timeout = setTimeout(later, wait); + + if (callNow) { + func.apply(context, args); + } + }; +}