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