react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / function / throttle.js
80742 viewsvar debounce = require('./debounce'),1isObject = require('../lang/isObject');23/** Used as the `TypeError` message for "Functions" methods. */4var FUNC_ERROR_TEXT = 'Expected a function';56/** Used as an internal `_.debounce` options object by `_.throttle`. */7var debounceOptions = {8'leading': false,9'maxWait': 0,10'trailing': false11};1213/**14* Creates a throttled function that only invokes `func` at most once per15* every `wait` milliseconds. The throttled function comes with a `cancel`16* method to cancel delayed invocations. Provide an options object to indicate17* that `func` should be invoked on the leading and/or trailing edge of the18* `wait` timeout. Subsequent calls to the throttled function return the19* result of the last `func` call.20*21* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked22* on the trailing edge of the timeout only if the the throttled function is23* invoked more than once during the `wait` timeout.24*25* See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)26* for details over the differences between `_.throttle` and `_.debounce`.27*28* @static29* @memberOf _30* @category Function31* @param {Function} func The function to throttle.32* @param {number} [wait=0] The number of milliseconds to throttle invocations to.33* @param {Object} [options] The options object.34* @param {boolean} [options.leading=true] Specify invoking on the leading35* edge of the timeout.36* @param {boolean} [options.trailing=true] Specify invoking on the trailing37* edge of the timeout.38* @returns {Function} Returns the new throttled function.39* @example40*41* // avoid excessively updating the position while scrolling42* jQuery(window).on('scroll', _.throttle(updatePosition, 100));43*44* // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes45* jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {46* 'trailing': false47* }));48*49* // cancel a trailing throttled call50* jQuery(window).on('popstate', throttled.cancel);51*/52function throttle(func, wait, options) {53var leading = true,54trailing = true;5556if (typeof func != 'function') {57throw new TypeError(FUNC_ERROR_TEXT);58}59if (options === false) {60leading = false;61} else if (isObject(options)) {62leading = 'leading' in options ? !!options.leading : leading;63trailing = 'trailing' in options ? !!options.trailing : trailing;64}65debounceOptions.leading = leading;66debounceOptions.maxWait = +wait;67debounceOptions.trailing = trailing;68return debounce(func, wait, debounceOptions);69}7071module.exports = throttle;727374