tui-code-snippet
Version:
TOAST UI Utility: CodeSnippet
59 lines (50 loc) • 1.52 kB
JavaScript
/**
* @fileoverview Creates a debounced function that delays invoking fn until after delay milliseconds has elapsed since the last time the debouced function was invoked.
* @author NHN FE Development Lab <dl_javascript.nhn.com>
*/
;
/**
* @module tricks
*/
/**
* Creates a debounced function that delays invoking fn until after delay milliseconds has elapsed
* since the last time the debouced function was invoked.
* @param {function} fn The function to debounce.
* @param {number} [delay=0] The number of milliseconds to delay
* @returns {function} debounced function.
* @memberof module:tricks
* @example
* // ES6
* import debounce from 'tui-code-snippet/tricks/debounce';
*
* // CommonJS
* const debounce = require('tui-code-snippet/tricks/debounce');
*
* function someMethodToInvokeDebounced() {}
*
* const debounced = debounce(someMethodToInvokeDebounced, 300);
*
* // invoke repeatedly
* debounced();
* debounced();
* debounced();
* debounced();
* debounced();
* debounced(); // last invoke of debounced()
*
* // invoke someMethodToInvokeDebounced() after 300 milliseconds.
*/
function debounce(fn, delay) {
var timer, args;
/* istanbul ignore next */
delay = delay || 0;
function debounced() { // eslint-disable-line require-jsdoc
args = Array.prototype.slice.call(arguments);
window.clearTimeout(timer);
timer = window.setTimeout(function() {
fn.apply(null, args);
}, delay);
}
return debounced;
}
module.exports = debounce;