vue3中使用 防抖和节流

41 阅读1分钟

默认有这个库 在需要的地方引入即可

import { debounce, throttle } from 'lodash'

监听的方法

function xxx(e) {}

在onMounted挂载

onMounted(async () => {
  // 监听滚动
  window.addEventListener('scroll', throttle(xxx,500), true);})

销毁页面时取消挂载

onBeforeUnmount(() => {
  window.removeEventListener('scroll', throttle(xxx,500), true);
});

只会在500ms内触发一次