Open
Description
<script setup lang="ts">
/**
* Implement the custom directive
* Make sure the `onClick` method only gets triggered once when clicked many times quickly
* And you also need to support the debounce delay time option. e.g `v-debounce-click:ms`
*
*/
const VDebounceClick = {
mounted(el, binding) {
function debounce(func, wait) {
let timeout;
return function () {
let context = this;
let args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
func.apply(context, args);
}, wait);
};
}
el.addEventListener('click', debounce(binding.value, binding.arg));
},
};
function onClick() {
console.log('Only triggered once when clicked many times quickly');
}
</script>
<template>
<button v-debounce-click:200="onClick">Click on it many times quickly</button>
</template>