@prettyy/ui
Version:
vue2 UI
26 lines (21 loc) • 528 B
JavaScript
const debounce = {
bind(el, binding) {
if (typeof binding.value !== 'function') {
throw new Error('callback must be a function')
}
let timer = null
el.__handleClick__ = function () {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
binding.value()
}, 500)
}
el.addEventListener('click', el.__handleClick__)
},
unbind(el) {
el.removeEventListener('click', el.__handleClick__)
},
}
export default debounce