import { Directive, DirectiveBinding } from "vue";

/**
 * 防抖-立即执行，一秒内只能点击一次
 */
export const debounce: Directive = {
  mounted(el: Element, binding: DirectiveBinding) {
    let timer: any;
    let flag = true; //是否执行绑定事件
    el.addEventListener(binding.arg + "", () => {
      clearTimeout(timer);
      if (flag) {
        binding.value();
        flag = false;
      }
      timer = setTimeout(() => {
        flag = true;
      }, 1000);
    });
  },
};

export const throttle: Directive = {
  // mounted(el: Element, binding: DirectiveBinding) { },
};
