UNPKG

links

Version:

Experimental content sharing and collaboration platform.

33 lines (27 loc) 732 B
/** * Debounces a function by the given threshold. * * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ * @param {Function} function to wrap * @param {Number} timeout in ms (`100`) * @param {Boolean} whether to execute at the beginning (`false`) * @api public */ module.exports = function debounce(func, threshold, execAsap){ var timeout; return function debounced(){ var obj = this, args = arguments; function delayed () { if (!execAsap) { func.apply(obj, args); } timeout = null; } if (timeout) { clearTimeout(timeout); } else if (execAsap) { func.apply(obj, args); } timeout = setTimeout(delayed, threshold || 100); }; };