Files
DjangoAdmin_Tornado_NaiveVue/ui/src/directives/throttle.ts
T
2026-09-16 12:24:11 +08:00

32 lines
766 B
TypeScript

import { DirectiveBinding } from 'vue';
let throttleTimer: NodeJS.Timeout | null;
export const throttle = {
mounted(el: HTMLElement, binding: DirectiveBinding) {
const eventType: string = Object.keys(binding.modifiers)[0] || 'click';
el.addEventListener(eventType, () => {
const dealy: number = binding.arg ? parseInt(binding.arg) : 300;
const fn: unknown = binding.value;
if (isNaN(dealy)) {
throw Error('v-throttle:arg必须为数字!');
}
if (typeof fn !== 'function') {
throw Error('v-throttle绑定值必须为函数!');
}
if (throttleTimer) {
return;
}
throttleTimer = setTimeout(() => {
fn();
throttleTimer = null;
}, dealy);
});
},
};