throttle.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* eslint-disable no-undefined,no-param-reassign,no-shadow */
  2. /**
  3. * Throttle execution of a function. Especially useful for rate limiting
  4. * execution of handlers on events like resize and scroll.
  5. *
  6. * @param {Number} delay A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  7. * @param {Boolean} [noTrailing] Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
  8. * throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
  9. * after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
  10. * the internal counter is reset)
  11. * @param {Function} callback A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  12. * to `callback` when the throttled-function is executed.
  13. * @param {Boolean} [debounceMode] If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
  14. * schedule `callback` to execute after `delay` ms.
  15. *
  16. * @return {Function} A new, throttled, function.
  17. */
  18. module.exports = function ( delay, noTrailing, callback, debounceMode ) {
  19. // After wrapper has stopped being called, this timeout ensures that
  20. // `callback` is executed at the proper times in `throttle` and `end`
  21. // debounce modes.
  22. var timeoutID;
  23. // Keep track of the last time `callback` was executed.
  24. var lastExec = 0;
  25. // `noTrailing` defaults to falsy.
  26. if ( typeof noTrailing !== 'boolean' ) {
  27. debounceMode = callback;
  28. callback = noTrailing;
  29. noTrailing = undefined;
  30. }
  31. // The `wrapper` function encapsulates all of the throttling / debouncing
  32. // functionality and when executed will limit the rate at which `callback`
  33. // is executed.
  34. function wrapper () {
  35. var self = this;
  36. var elapsed = Number(new Date()) - lastExec;
  37. var args = arguments;
  38. // Execute `callback` and update the `lastExec` timestamp.
  39. function exec () {
  40. lastExec = Number(new Date());
  41. callback.apply(self, args);
  42. }
  43. // If `debounceMode` is true (at begin) this is used to clear the flag
  44. // to allow future `callback` executions.
  45. function clear () {
  46. timeoutID = undefined;
  47. }
  48. if ( debounceMode && !timeoutID ) {
  49. // Since `wrapper` is being called for the first time and
  50. // `debounceMode` is true (at begin), execute `callback`.
  51. exec();
  52. }
  53. // Clear any existing timeout.
  54. if ( timeoutID ) {
  55. clearTimeout(timeoutID);
  56. }
  57. if ( debounceMode === undefined && elapsed > delay ) {
  58. // In throttle mode, if `delay` time has been exceeded, execute
  59. // `callback`.
  60. exec();
  61. } else if ( noTrailing !== true ) {
  62. // In trailing throttle mode, since `delay` time has not been
  63. // exceeded, schedule `callback` to execute `delay` ms after most
  64. // recent execution.
  65. //
  66. // If `debounceMode` is true (at begin), schedule `clear` to execute
  67. // after `delay` ms.
  68. //
  69. // If `debounceMode` is false (at end), schedule `callback` to
  70. // execute after `delay` ms.
  71. timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
  72. }
  73. }
  74. // Return the wrapper function.
  75. return wrapper;
  76. };