throttle.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.default = (function (func, wait, options) {
  4. var timeout, context, args, result;
  5. var previous = 0;
  6. if (!options)
  7. options = {};
  8. var later = function () {
  9. previous = options.leading === false ? 0 : Date.now();
  10. timeout = null;
  11. result = func.apply(context, args);
  12. if (!timeout)
  13. context = args = null;
  14. };
  15. var throttled = function () {
  16. var now = Date.now();
  17. if (!previous && options.leading === false)
  18. previous = now;
  19. var remaining = wait - (now - previous);
  20. context = this;
  21. args = arguments;
  22. if (remaining <= 0 || remaining > wait) {
  23. if (timeout) {
  24. clearTimeout(timeout);
  25. timeout = null;
  26. }
  27. previous = now;
  28. result = func.apply(context, args);
  29. if (!timeout)
  30. context = args = null;
  31. }
  32. else if (!timeout && options.trailing !== false) {
  33. timeout = setTimeout(later, remaining);
  34. }
  35. return result;
  36. };
  37. throttled.cancel = function () {
  38. clearTimeout(timeout);
  39. previous = 0;
  40. timeout = context = args = null;
  41. };
  42. return throttled;
  43. });
  44. //# sourceMappingURL=throttle.js.map