throttle.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.throttle = void 0;
  4. /**
  5. * 节流修饰器
  6. * @param delay 节流时间
  7. */
  8. function throttle(delay, rightNow) {
  9. if (delay === void 0) { delay = 0; }
  10. if (rightNow === void 0) { rightNow = false; }
  11. return function (target, propertyKey, descriptor) {
  12. var func = descriptor.value;
  13. var timeout;
  14. if (typeof func === 'function') {
  15. // eslint-disable-next-line
  16. descriptor.value = function () {
  17. var args = [];
  18. for (var _i = 0; _i < arguments.length; _i++) {
  19. args[_i] = arguments[_i];
  20. }
  21. if (timeout)
  22. return;
  23. var context = this;
  24. if (rightNow)
  25. func.apply(context, args);
  26. timeout = window.setTimeout(function () {
  27. func.apply(context, args);
  28. timeout = null;
  29. }, delay);
  30. };
  31. }
  32. };
  33. }
  34. exports.throttle = throttle;
  35. //# sourceMappingURL=throttle.js.map