throttle.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var ORIGIN_METHOD = '\0__throttleOriginMethod';
  2. var RATE = '\0__throttleRate';
  3. var THROTTLE_TYPE = '\0__throttleType';
  4. /**
  5. * @public
  6. * @param {(Function)} fn
  7. * @param {number} [delay=0] Unit: ms.
  8. * @param {boolean} [debounce=false]
  9. * true: If call interval less than `delay`, only the last call works.
  10. * false: If call interval less than `delay, call works on fixed rate.
  11. * @return {(Function)} throttled fn.
  12. */
  13. function throttle(fn, delay, debounce) {
  14. var currCall;
  15. var lastCall = 0;
  16. var lastExec = 0;
  17. var timer = null;
  18. var diff;
  19. var scope;
  20. var args;
  21. var debounceNextCall;
  22. delay = delay || 0;
  23. function exec() {
  24. lastExec = new Date().getTime();
  25. timer = null;
  26. fn.apply(scope, args || []);
  27. }
  28. var cb = function () {
  29. currCall = new Date().getTime();
  30. scope = this;
  31. args = arguments;
  32. var thisDelay = debounceNextCall || delay;
  33. var thisDebounce = debounceNextCall || debounce;
  34. debounceNextCall = null;
  35. diff = currCall - (thisDebounce ? lastCall : lastExec) - thisDelay;
  36. clearTimeout(timer);
  37. if (thisDebounce) {
  38. timer = setTimeout(exec, thisDelay);
  39. } else {
  40. if (diff >= 0) {
  41. exec();
  42. } else {
  43. timer = setTimeout(exec, -diff);
  44. }
  45. }
  46. lastCall = currCall;
  47. };
  48. /**
  49. * Clear throttle.
  50. * @public
  51. */
  52. cb.clear = function () {
  53. if (timer) {
  54. clearTimeout(timer);
  55. timer = null;
  56. }
  57. };
  58. /**
  59. * Enable debounce once.
  60. */
  61. cb.debounceNextCall = function (debounceDelay) {
  62. debounceNextCall = debounceDelay;
  63. };
  64. return cb;
  65. }
  66. /**
  67. * Create throttle method or update throttle rate.
  68. *
  69. * @example
  70. * ComponentView.prototype.render = function () {
  71. * ...
  72. * throttle.createOrUpdate(
  73. * this,
  74. * '_dispatchAction',
  75. * this.model.get('throttle'),
  76. * 'fixRate'
  77. * );
  78. * };
  79. * ComponentView.prototype.remove = function () {
  80. * throttle.clear(this, '_dispatchAction');
  81. * };
  82. * ComponentView.prototype.dispose = function () {
  83. * throttle.clear(this, '_dispatchAction');
  84. * };
  85. *
  86. * @public
  87. * @param {Object} obj
  88. * @param {string} fnAttr
  89. * @param {number} [rate]
  90. * @param {string} [throttleType='fixRate'] 'fixRate' or 'debounce'
  91. * @return {Function} throttled function.
  92. */
  93. function createOrUpdate(obj, fnAttr, rate, throttleType) {
  94. var fn = obj[fnAttr];
  95. if (!fn) {
  96. return;
  97. }
  98. var originFn = fn[ORIGIN_METHOD] || fn;
  99. var lastThrottleType = fn[THROTTLE_TYPE];
  100. var lastRate = fn[RATE];
  101. if (lastRate !== rate || lastThrottleType !== throttleType) {
  102. if (rate == null || !throttleType) {
  103. return obj[fnAttr] = originFn;
  104. }
  105. fn = obj[fnAttr] = throttle(originFn, rate, throttleType === 'debounce');
  106. fn[ORIGIN_METHOD] = originFn;
  107. fn[THROTTLE_TYPE] = throttleType;
  108. fn[RATE] = rate;
  109. }
  110. return fn;
  111. }
  112. /**
  113. * Clear throttle. Example see throttle.createOrUpdate.
  114. *
  115. * @public
  116. * @param {Object} obj
  117. * @param {string} fnAttr
  118. */
  119. function clear(obj, fnAttr) {
  120. var fn = obj[fnAttr];
  121. if (fn && fn[ORIGIN_METHOD]) {
  122. obj[fnAttr] = fn[ORIGIN_METHOD];
  123. }
  124. }
  125. exports.throttle = throttle;
  126. exports.createOrUpdate = createOrUpdate;
  127. exports.clear = clear;