diff --git a/assets/scripts/utils/debounce.js b/assets/scripts/utils/debounce.js new file mode 100644 index 0000000..40b14af --- /dev/null +++ b/assets/scripts/utils/debounce.js @@ -0,0 +1,15 @@ +export default function(func, wait, immediate) { + let timeout; + return function() { + const context = this; + const args = arguments; + const later = function() { + timeout = null; + if (!immediate) func.apply(context, args); + }; + const callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; +}