webfansplz / vuejs-challenges

Collection of Vue.js challenges
https://vuejs-challenges.netlify.app/
MIT License
2.71k stars 187 forks source link

20 - v-debounce-click #2810

Open kai-phan opened 1 month ago

kai-phan commented 1 month ago
const VDebounceClick = {
  mounted(el, binding) {
    const { arg, value } = binding;
    const num = Number(arg);

    let timer;

    el.onclick = (e) => {
      clearTimeout(timer);

      timer = setTimeout(() => {
        value(e)
      }, num)
    }

  }
}