節流和防抖

<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>
相關文章
相關標籤/搜索