debthrot.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * UNDERSCORE.JS 部分源码(有修改) 防抖节流
  3. */
  4. var restArguments = function (func, startIndex) {
  5. startIndex = startIndex == null ? func.length - 1 : +startIndex;
  6. return function () {
  7. var length = Math.max(arguments.length - startIndex, 0),
  8. rest = Array(length),
  9. index = 0;
  10. for (; index < length; index++) {
  11. rest[index] = arguments[index + startIndex];
  12. }
  13. switch (startIndex) {
  14. case 0: return func.call(this, rest);
  15. case 1: return func.call(this, arguments[0], rest);
  16. case 2: return func.call(this, arguments[0], arguments[1], rest);
  17. }
  18. var args = Array(startIndex + 1);
  19. for (index = 0; index < startIndex; index++) {
  20. args[index] = arguments[index];
  21. }
  22. args[startIndex] = rest;
  23. return func.apply(this, args);
  24. };
  25. };
  26. var delay = restArguments(function (func, wait, args) {
  27. return setTimeout(function () {
  28. return func.apply(null, args);
  29. }, wait);
  30. });
  31. var now = Date.now || function () {
  32. return new Date().getTime();
  33. };
  34. /*
  35. 默认
  36. 在 wait 间隔结束时,将使用最近传递给 debounced(去抖动)函数的参数调用该函数
  37. 参数:
  38. immediate 为 true, debounce会在 wait 时间间隔的开始调用这个函数,反之在 wait 间隔结束时调用这个函数
  39. 属性
  40. cancel:如果需要取消预定的 debounce ,可以在 debounce 函数上调用 .cancel()。
  41. 返回值类型
  42. Function
  43. */
  44. var debounce = function (func, wait, immediate) {
  45. var timeout, result;
  46. var later = function (context, args) {
  47. timeout = null;
  48. if (args) result = func.apply(context, args);
  49. };
  50. var debounced = restArguments(function (args) {
  51. if (timeout) clearTimeout(timeout);
  52. if (immediate) {
  53. var callNow = !timeout;
  54. timeout = setTimeout(later, wait);
  55. if (callNow) result = func.apply(this, args);
  56. } else {
  57. timeout = delay(later, wait, this, args);
  58. }
  59. return result;
  60. });
  61. debounced.cancel = function () {
  62. clearTimeout(timeout);
  63. timeout = null;
  64. };
  65. return debounced;
  66. };
  67. /*
  68. 默认
  69. 在你调用的第一时间尽快执行这个function,并且,如果你在wait周期内调用任意次数的函数,都将尽快的被覆盖。
  70. 参数
  71. options:{
  72. leading: 如果你想禁用第一次首先执行的话,传递 {leading: false}
  73. trailing: 如果你想禁用最后一次执行的话,传递 {trailing: false}
  74. }
  75. 属性
  76. cancel:如果需要取消预定的 throttle ,可以在 throttle 函数上调用 .cancel()。
  77. 返回值类型
  78. Function
  79. */
  80. var throttle = function (func, wait, options) {
  81. var timeout, context, args, result;
  82. var previous = 0;
  83. if (!options) options = {};
  84. var later = function () {
  85. previous = options.leading === false ? 0 : now();
  86. timeout = null;
  87. result = func.apply(context, args);
  88. if (!timeout) context = args = null;
  89. };
  90. var throttled = function () {
  91. var now = now();
  92. if (!previous && options.leading === false) previous = now;
  93. var remaining = wait - (now - previous);
  94. context = this;
  95. args = arguments;
  96. if (remaining <= 0 || remaining > wait) {
  97. if (timeout) {
  98. clearTimeout(timeout);
  99. timeout = null;
  100. }
  101. previous = now;
  102. result = func.apply(context, args);
  103. if (!timeout) context = args = null;
  104. } else if (!timeout && options.trailing !== false) {
  105. timeout = setTimeout(later, remaining);
  106. }
  107. return result;
  108. };
  109. throttled.cancel = function () {
  110. clearTimeout(timeout);
  111. previous = 0;
  112. timeout = context = args = null;
  113. };
  114. return throttled;
  115. };
  116. export { debounce, throttle };