Throttle.ts 507 B

123456789101112131415161718192021
  1. /**
  2. * 方法节流
  3. * @param wait 节流时间 单位秒
  4. */
  5. export default function Throttle(wait: number) {
  6. return function (target: any, keyname: string, descriptor: PropertyDescriptor) {
  7. const method: Function = descriptor.value;
  8. let timer: number = null;
  9. descriptor.value = function (...args: any[]) {
  10. if (timer) {
  11. return;
  12. } else {
  13. timer = setInterval(() => {
  14. clearInterval(timer);
  15. timer = null;
  16. }, wait * 1000);
  17. return method.apply(this, args);
  18. }
  19. };
  20. };
  21. }