39 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-12-19 18:02:29 +08:00
/*
使
N秒内只执行一次N秒内重复调用了多少次
1
2
使 Dom v-throttle
<el-button type="danger" icon="refresh" plain v-throttle="resetSearch"></el-button>
*/
// @ts-nocheck
import type { Directive, DirectiveBinding } from "vue";
interface ElType extends HTMLElement {
handleClick: () => any;
disabled: boolean;
}
const throttle: Directive = {
mounted(el: ElType, binding: DirectiveBinding) {
let timer = null;
const delay = parseInt(binding.arg) || 500;
const handler = binding.value;
let lastExecTime = 0;
el.addEventListener(
"click",
() => {
const now = Date.now();
if (now - lastExecTime > delay) {
handler();
lastExecTime = now;
}
},
{ passive: false }
);
}
};
export default throttle;