throttle.js 1000 B

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