index.ts 605 B

12345678910111213141516171819202122232425
  1. import { Directive, DirectiveBinding } from "vue";
  2. /**
  3. * 防抖-立即执行,一秒内只能点击一次
  4. */
  5. export const debounce: Directive = {
  6. mounted(el: Element, binding: DirectiveBinding) {
  7. let timer: any;
  8. let flag = true; //是否执行绑定事件
  9. el.addEventListener(binding.arg + "", () => {
  10. clearTimeout(timer);
  11. if (flag) {
  12. binding.value();
  13. flag = false;
  14. }
  15. timer = setTimeout(() => {
  16. flag = true;
  17. }, 1000);
  18. });
  19. },
  20. };
  21. export const throttle: Directive = {
  22. // mounted(el: Element, binding: DirectiveBinding) { },
  23. };