mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.5 KiB
62 lines
2.5 KiB
10 years ago
|
var debounce = require('./debounce'),
|
||
9 years ago
|
isObject = require('./isObject');
|
||
10 years ago
|
|
||
|
/** Used as the `TypeError` message for "Functions" methods. */
|
||
|
var FUNC_ERROR_TEXT = 'Expected a function';
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Creates a throttled function that only invokes `func` at most once per
|
||
|
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
||
9 years ago
|
* method to cancel delayed `func` invocations and a `flush` method to
|
||
|
* immediately invoke them. Provide an options object to indicate whether
|
||
|
* `func` should be invoked on the leading and/or trailing edge of the `wait`
|
||
|
* timeout. The `func` is invoked with the last arguments provided to the
|
||
|
* throttled function. Subsequent calls to the throttled function return the
|
||
|
* result of the last `func` invocation.
|
||
10 years ago
|
*
|
||
|
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
|
||
9 years ago
|
* on the trailing edge of the timeout only if the throttled function is
|
||
10 years ago
|
* invoked more than once during the `wait` timeout.
|
||
|
*
|
||
|
* See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
|
||
|
* for details over the differences between `_.throttle` and `_.debounce`.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @category Function
|
||
|
* @param {Function} func The function to throttle.
|
||
|
* @param {number} [wait=0] The number of milliseconds to throttle invocations to.
|
||
|
* @param {Object} [options] The options object.
|
||
|
* @param {boolean} [options.leading=true] Specify invoking on the leading
|
||
|
* edge of the timeout.
|
||
|
* @param {boolean} [options.trailing=true] Specify invoking on the trailing
|
||
|
* edge of the timeout.
|
||
|
* @returns {Function} Returns the new throttled function.
|
||
|
* @example
|
||
|
*
|
||
9 years ago
|
* // Avoid excessively updating the position while scrolling.
|
||
10 years ago
|
* jQuery(window).on('scroll', _.throttle(updatePosition, 100));
|
||
|
*
|
||
9 years ago
|
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
||
|
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
|
||
|
* jQuery(element).on('click', throttled);
|
||
10 years ago
|
*
|
||
9 years ago
|
* // Cancel the trailing throttled invocation.
|
||
10 years ago
|
* jQuery(window).on('popstate', throttled.cancel);
|
||
|
*/
|
||
|
function throttle(func, wait, options) {
|
||
|
var leading = true,
|
||
|
trailing = true;
|
||
|
|
||
|
if (typeof func != 'function') {
|
||
|
throw new TypeError(FUNC_ERROR_TEXT);
|
||
|
}
|
||
9 years ago
|
if (isObject(options)) {
|
||
10 years ago
|
leading = 'leading' in options ? !!options.leading : leading;
|
||
|
trailing = 'trailing' in options ? !!options.trailing : trailing;
|
||
|
}
|
||
9 years ago
|
return debounce(func, wait, { 'leading': leading, 'maxWait': wait, 'trailing': trailing });
|
||
10 years ago
|
}
|
||
|
|
||
|
module.exports = throttle;
|