跳至主要內容

防抖与节流

ZiHao...小于 1 分钟记录javascript

防抖与节流

复习一下

function debounce(fn, delay) {
  let timerID = null;
  return function () {
    const context = this;
    if (timeID) {
      window.clearTimeout(timerID);
    }
    timerID = setTimeout(() => {
      fn.apply(context, arguments);
    }, delay);
  };
}
function thottle(fn) {
  /* 节流 */
  let cardu = true;
  if (cardu) {
    fn.apply(this, arguments);
    cardu = false;
    setTimeout(() => {
      cardu = true;
    }, 3000);
  }
}
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5