自学内容网 自学内容网

防抖和节流

防抖

 function debounce(fn,delay){
            let t = null
          return function(){
            if(t!==null){
                clearTimeout(t)
            }
            t= setTimeout(() =>{
                fn.call(this)
            },delay)
        }
        }

频繁触发事件,只执行事件的最后一次

其中变量t的使用实际是闭包的实际使用

节流

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

频繁触发事件,在规定时间内,只执行一次


原文地址:https://blog.csdn.net/weixin_68854196/article/details/136357575

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!