节流

 1 //节流
 2 const throttle=(function(){
 3   var firstTime=0;
 4   var timer=null;
 5   return function(callback,time=300){
 6     var lastTime=new Date().getTime()
 7     clearTimeout(timer);
 8     if(lastTime-firstTime>time){
 9       callback();
10       firstTime=lastTime;
11     }else{
12       timer=setTimeout(()=>{
13           callback()
14       },time)
15     }
16   }
17 })()

 

posted @ 2020-05-26 22:17  king-xin  阅读(153)  评论(0)    收藏  举报