index.js 7.5 KB

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