1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 1 1 4 4 12 12 6 2 6 12 6 6 4 12 12 4 1 | define(function () { /** * Debounce callback execution */ function debounce(fn, threshold, isAsap){ var timeout, result; function debounced(){ var args = arguments, context = this; function delayed(){ if (! isAsap) { result = fn.apply(context, args); } timeout = null; } if (timeout) { clearTimeout(timeout); } else if (isAsap) { result = fn.apply(context, args); } timeout = setTimeout(delayed, threshold); return result; } return debounced; } return debounce; }); |