js实现节流防抖动

代码:

<body>
        <button type="button"id="btn">点击</button>
    </body>
    <script type="text/javascript">
        let btn=document.getElementById("btn")
        
        
        function debounce(func,await){
            let timer;
            return function(){
                let context=this;
                let args=arguments;
                if(timer){
                    clearTimeout(timer)
                }
                timer=setTimeout(()=>{
                    func.call(context,args)
                },await)
            }
            
        }
        
        btn.onclick=debounce(function(){
            console.log("hello world")
        },1000)
    </script>

 

posted @ 2020-05-26 10:33  山吹同学  阅读(899)  评论(0编辑  收藏  举报