[d565449] | 1 | /* eslint-disable no-undefined,no-param-reassign,no-shadow */
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Throttle execution of a function. Especially useful for rate limiting
|
---|
| 5 | * execution of handlers on events like resize and scroll.
|
---|
| 6 | *
|
---|
| 7 | * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
|
---|
| 8 | * @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
|
---|
| 9 | * throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
|
---|
| 10 | * after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
|
---|
| 11 | * the internal counter is reset).
|
---|
| 12 | * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
|
---|
| 13 | * to `callback` when the throttled-function is executed.
|
---|
| 14 | * @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
|
---|
| 15 | * schedule `callback` to execute after `delay` ms.
|
---|
| 16 | *
|
---|
| 17 | * @returns {Function} A new, throttled, function.
|
---|
| 18 | */
|
---|
| 19 | function throttle (delay, noTrailing, callback, debounceMode) {
|
---|
| 20 | /*
|
---|
| 21 | * After wrapper has stopped being called, this timeout ensures that
|
---|
| 22 | * `callback` is executed at the proper times in `throttle` and `end`
|
---|
| 23 | * debounce modes.
|
---|
| 24 | */
|
---|
| 25 | var timeoutID;
|
---|
| 26 | var cancelled = false; // Keep track of the last time `callback` was executed.
|
---|
| 27 |
|
---|
| 28 | var lastExec = 0; // Function to clear existing timeout
|
---|
| 29 |
|
---|
| 30 | function clearExistingTimeout() {
|
---|
| 31 | if (timeoutID) {
|
---|
| 32 | clearTimeout(timeoutID);
|
---|
| 33 | }
|
---|
| 34 | } // Function to cancel next exec
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | function cancel() {
|
---|
| 38 | clearExistingTimeout();
|
---|
| 39 | cancelled = true;
|
---|
| 40 | } // `noTrailing` defaults to falsy.
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | if (typeof noTrailing !== 'boolean') {
|
---|
| 44 | debounceMode = callback;
|
---|
| 45 | callback = noTrailing;
|
---|
| 46 | noTrailing = undefined;
|
---|
| 47 | }
|
---|
| 48 | /*
|
---|
| 49 | * The `wrapper` function encapsulates all of the throttling / debouncing
|
---|
| 50 | * functionality and when executed will limit the rate at which `callback`
|
---|
| 51 | * is executed.
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | function wrapper() {
|
---|
| 56 | for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
|
---|
| 57 | arguments_[_key] = arguments[_key];
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | var self = this;
|
---|
| 61 | var elapsed = Date.now() - lastExec;
|
---|
| 62 |
|
---|
| 63 | if (cancelled) {
|
---|
| 64 | return;
|
---|
| 65 | } // Execute `callback` and update the `lastExec` timestamp.
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | function exec() {
|
---|
| 69 | lastExec = Date.now();
|
---|
| 70 | callback.apply(self, arguments_);
|
---|
| 71 | }
|
---|
| 72 | /*
|
---|
| 73 | * If `debounceMode` is true (at begin) this is used to clear the flag
|
---|
| 74 | * to allow future `callback` executions.
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | function clear() {
|
---|
| 79 | timeoutID = undefined;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (debounceMode && !timeoutID) {
|
---|
| 83 | /*
|
---|
| 84 | * Since `wrapper` is being called for the first time and
|
---|
| 85 | * `debounceMode` is true (at begin), execute `callback`.
|
---|
| 86 | */
|
---|
| 87 | exec();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | clearExistingTimeout();
|
---|
| 91 |
|
---|
| 92 | if (debounceMode === undefined && elapsed > delay) {
|
---|
| 93 | /*
|
---|
| 94 | * In throttle mode, if `delay` time has been exceeded, execute
|
---|
| 95 | * `callback`.
|
---|
| 96 | */
|
---|
| 97 | exec();
|
---|
| 98 | } else if (noTrailing !== true) {
|
---|
| 99 | /*
|
---|
| 100 | * In trailing throttle mode, since `delay` time has not been
|
---|
| 101 | * exceeded, schedule `callback` to execute `delay` ms after most
|
---|
| 102 | * recent execution.
|
---|
| 103 | *
|
---|
| 104 | * If `debounceMode` is true (at begin), schedule `clear` to execute
|
---|
| 105 | * after `delay` ms.
|
---|
| 106 | *
|
---|
| 107 | * If `debounceMode` is false (at end), schedule `callback` to
|
---|
| 108 | * execute after `delay` ms.
|
---|
| 109 | */
|
---|
| 110 | timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | wrapper.cancel = cancel; // Return the wrapper function.
|
---|
| 115 |
|
---|
| 116 | return wrapper;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /* eslint-disable no-undefined */
|
---|
| 120 | /**
|
---|
| 121 | * Debounce execution of a function. Debouncing, unlike throttling,
|
---|
| 122 | * guarantees that a function is only executed a single time, either at the
|
---|
| 123 | * very beginning of a series of calls, or at the very end.
|
---|
| 124 | *
|
---|
| 125 | * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
|
---|
| 126 | * @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
|
---|
| 127 | * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
|
---|
| 128 | * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
|
---|
| 129 | * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
|
---|
| 130 | * to `callback` when the debounced-function is executed.
|
---|
| 131 | *
|
---|
| 132 | * @returns {Function} A new, debounced function.
|
---|
| 133 | */
|
---|
| 134 |
|
---|
| 135 | function debounce (delay, atBegin, callback) {
|
---|
| 136 | return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | export { debounce, throttle };
|
---|
| 140 | //# sourceMappingURL=index.js.map
|
---|