节流、防抖

// 节流
function throttle(fn, delay, asThis) {
  let timer = null;
  return (...args) => {
    if (timer) return
    fn.call(asThis, ...args)
    timer = setTimeout(() => {
      timer = false;
    }, delay)
  }
}

// 防抖
function debounce(fn, delay, asThis) {
  let timer = null;
  return (...args) => {
    timer && clearTimeout(timer)
    timer = setTimeout(() => {
      fn.call(asThis, ...args)
      timer = null;
    }, delay)
  }
}
posted @ 2024-06-06 13:27  codejnp  阅读(2)  评论(0编辑  收藏  举报