diff --git a/frappe/public/js/frappe/utils/utils.js b/frappe/public/js/frappe/utils/utils.js index 7187bb8077..01636c32bc 100644 --- a/frappe/public/js/frappe/utils/utils.js +++ b/frappe/public/js/frappe/utils/utils.js @@ -883,19 +883,40 @@ Object.assign(frappe.utils, { }; }, debounce: function (func, wait, immediate) { - var timeout; - return function () { - var context = this, - args = arguments; - var later = function () { - timeout = null; - if (!immediate) func.apply(context, args); - }; + var timeout, context, args; + + var later = function () { + timeout = null; + if (!immediate) func.apply(context, args); + }; + + var debounced = function () { + context = this; + args = arguments; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; + + debounced.cancel = function () { + if (!timeout) return false; + + clearTimeout(timeout); + timeout = null; + return true; + }; + + debounced.flush = function () { + if (!timeout) return false; + + clearTimeout(timeout); + timeout = null; + func.apply(context, args); + return true; + }; + + return debounced; }, get_form_link: function ( doctype,