index.umd.js 5.9 KB

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