From 843fee62e2e0ed4064b180fef864ec9926d430fb Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 16 Jan 2018 16:01:45 -0500 Subject: [PATCH] utils.debounce --- public/src/utils.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/public/src/utils.js b/public/src/utils.js index 078396fdeb..2fcdfa8157 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -746,6 +746,27 @@ rtrim: function (str) { return str.replace(/\s+$/g, ''); }, + + debounce: function (func, wait, immediate) { + // modified from https://davidwalsh.name/javascript-debounce-function + var timeout; + return function () { + var context = this; + var args = arguments; + var later = function () { + timeout = null; + if (!immediate) { + func.apply(context, args); + } + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) { + func.apply(context, args); + } + }; + }, }; /* eslint "no-extend-native": "off" */