以下是一个简单的 Vue 防抖指令的实现:

Vue.directive('debounce', {
  inserted: function (el, binding) {
    let timeout = null;
    el.addEventListener('input', () => {
      clearTimeout(timeout);
      timeout = setTimeout(() => {
        binding.value();
      }, binding.arg || 500);
    });
  }
});

使用方法:

<input v-debounce:500="handleInput">

其中 handleInput 是一个函数,表示要防抖的操作。:500 表示防抖的时间间隔为 500 毫秒。


原文地址: https://www.cveoy.top/t/topic/bgj2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录