2025-03-JS-SDK-CLI/src/directive/clickThrottle.ts
李兴辉 b7a8553bbf init
2025-03-10 13:48:17 +08:00

22 lines
622 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Directive } from 'vue';
/**
* 按钮节流指令
*/
const ThrottleDirective: Directive = {
beforeMount(el, binding: any) {
let throttling = false;
el.addEventListener('click', () => {
console.log(binding, 'binding')
if (!throttling) {
throttling = true;
setTimeout(() => {
throttling = false;
}, parseInt(binding.arg) || 300); // 默认节流时间为300毫秒也可以从参数中传递
binding.value();
}
});
}
};
export default ThrottleDirective