mirror of
http://git.xinwangdao.com/cnnc-embedded-parts-detect/detect-embeded.git
synced 2025-06-24 21:44:12 +08:00
32 lines
880 B
TypeScript
32 lines
880 B
TypeScript
/**
|
|
* Prevent repeated clicks
|
|
* @Example v-repeat-click="()=>{}"
|
|
*/
|
|
import { on, once } from '/@/utils/domUtils';
|
|
import type { Directive, DirectiveBinding } from 'vue';
|
|
|
|
const repeatDirective: Directive = {
|
|
beforeMount(el: Element, binding: DirectiveBinding<any>) {
|
|
let interval: Nullable<IntervalHandle> = null;
|
|
let startTime = 0;
|
|
const handler = (): void => binding?.value();
|
|
const clear = (): void => {
|
|
if (Date.now() - startTime < 100) {
|
|
handler();
|
|
}
|
|
interval && clearInterval(interval);
|
|
interval = null;
|
|
};
|
|
|
|
on(el, 'mousedown', (e: Event): void => {
|
|
if ((e as MouseEvent).button !== 0) return;
|
|
startTime = Date.now();
|
|
once(document as any, 'mouseup', clear);
|
|
interval && clearInterval(interval);
|
|
interval = setInterval(handler, 100);
|
|
});
|
|
},
|
|
};
|
|
|
|
export default repeatDirective;
|