js工具函数,自己封装一个节流函数

2022-06-23  本文已影响0人  前端蜗牛老师
/**
 *
 * @param fn 节流的函数
 * @param delay 延迟时间
 * @returns 节流过的函数
 */
let debounceTimer = ''
export function debounce(fn, delay = 700) {
  clearTimeout(debounceTimer)
  debounceTimer = setTimeout(() => {
    fn()
  }, delay)
}

使用非常简单

 debounce(this.getQuery, 600)

还有很多方法,请评论区留言接龙,写出你的方法

上一篇 下一篇

猜你喜欢

热点阅读