笔记整理 js防抖函数和节流函数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43414945/article/details/89521091
    //           //防抖
        window.οnscrοll=antiShake(function(){
            console.log("我顶你个非啊")
        },100)

    function antiShake(func,delay){
                   var timer=null
                return function(){
                    clearInterval(timer);
                     timer= setTimeout(function(){
                          func();
                     },delay)
                }
      };
      //函数的节流
      window.οnscrοll=throttle(function(){
               console.log("我顶你个肺");
      });
      function throttle(func){
          
          var d=new Date();
          var firstTime=d.getTime();
          return function(){
             // console.log("11111");
              var now=new Date().getTime();
              if(now-firstTime>500){
                   func();
                   firstTime=now;
              }
          }
      }

    

    </script>

猜你喜欢

转载自blog.csdn.net/weixin_43414945/article/details/89521091