const debounce = function(func, wait, immediate = true) {
let timeout;
return function() {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait);
if (callNow) func.apply(this, arguments);
} else {
timeout = setTimeout(function() {
func.apply(context, args);
}, wait);
}
};
};
Vue.directive('debounce', {
bind(el, binding) {
const func = debounce(binding.value, 1000);
el.addEventListener('click', func);
},
});
<div v-debounce="onRun" v-show="!isStart">
<span>开始</span>
</div>
onRun() {
this.isStart = true
},