JS防抖节流简单实现

179 阅读1分钟

防抖

        //防抖
        function debounce(fn,delay){
            let timer = null
            
            return function(){
                if (timer) clearTimeout(timer)
                timer = setTimeout( ()=>{
                    fn.call(this, ...arguments)
                },delay)
            }
            
        }

节流

        function throttle(fn,delay){
            let flag = true
            return function (){
                if(flag){
                    setTimeout(()=>{
                        fn.call(this,...arguments)
                        flag = true
                    },delay)
                }
                flag = false
            }

        }