From fe81a7dba929669bc1e28bac1fe226f331ad83eb Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 18 Jan 2020 04:14:10 -0500 Subject: [PATCH] Added debounce util function --- functions/utils/debounce.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 functions/utils/debounce.js diff --git a/functions/utils/debounce.js b/functions/utils/debounce.js new file mode 100644 index 000000000..09acda07d --- /dev/null +++ b/functions/utils/debounce.js @@ -0,0 +1,15 @@ +// Debounce is a higher order function which makes its enclosed function be executed +// only if the function wasn't called again till 'delay' time has passed, this helps reduce impact of heavy working +// functions which might be called frequently +// NOTE : Don't use lambda functions as this doesn't get bound properly in them, use the 'function (args) {}' format +const debounce = (func, delay) => { + let inDebounce + return function() { + const context = this + const args = arguments + clearTimeout(inDebounce) + inDebounce = setTimeout(() => func.apply(context, args), delay) + } +} + +export default debounce;