@prettyy/ui
Version:
vue2 UI
30 lines (25 loc) • 628 B
JavaScript
const throttle = {
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)
}
if (!el.disabled) {
el.disabled = true
binding.value()
timer = setTimeout(() => {
el.disabled = false
}, 2000)
}
}
el.addEventListener('click', el.__handleClick__)
},
unbind(el) {
el.removeEventListener('click', el.__handleClick__)
},
}
export default throttle