节流和防抖

<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>

  

原文地址:https://www.cnblogs.com/r-mp/p/11545527.html