节流和防抖

<script>
//防抖
var timer = null;
document.onscroll = function(){
    clearTimeout(timer)
   timer = setTimeout(()=>{
    console.log(111);
   },500)
}

//节流
var firstTime = 0;
document.onscroll = function(){
    var lastTime = new Date().getTime();

    if(lastTime - firstTime>500){
        console.log(111);
        firstTime = new Date().getTime();
    }
   
}
</script>

  

posted @ 2019-09-18 21:38  前端小菜鸟吖  阅读(316)  评论(0编辑  收藏  举报