前端节流的简单笔记

前端节流经常在项目监听页面滚动的时候用到,就是在一段时间内,只执行一次
话不多说,看代码

function throttle(func, deley) {
    let run = true,timer;
    return function () {
      if (!run) return;
      run = false; 
      func.apply(this, arguments)
      timer = setTimeout(() => {
        run = true 
        clearTimeout(timer);
      }, deley)
    }
  }

document.body.addEventListener('mousemove',throttle(e=>{console.log(e)},3000))
原文地址:https://www.cnblogs.com/live-to-talk/p/12788222.html