37 lines
788 B
TypeScript
Raw Permalink Normal View History

2024-11-05 10:09:18 +08:00
import type { Ref } from 'vue'
export const useLongPress = <T extends any[]>(
event: (...args: T) => void,
enabled: Ref<boolean>,
longPressIntervel = 250
) => {
// 长按判断定时器
let longPressTimer: ReturnType<typeof setTimeout> | null = null
// 清除长按判断定时器
const clearLongPressTimer = () => {
if (longPressTimer) {
clearInterval(longPressTimer)
longPressTimer = null
}
}
// 处理长按事件
const handleLongPressEvent = (...args: T) => {
if (enabled.value) {
event(...args)
clearLongPressTimer()
longPressTimer = setInterval(() => {
event(...args)
}, longPressIntervel)
} else {
event(...args)
}
}
return {
handleLongPressEvent,
clearLongPressTimer,
}
}