• 节流(Throttling) & 防抖(Debouncing)


    •  执行函数
    function doSth(sth){
        console.log(sth)
    }
    
    • 防抖
    function debounce(fn,wait){
        let timer
        return (...args)=>{
            clearTimeout(timer)
            timer = setTimeout(()=>{
                fn.apply(this,args)
            },wait)
        }
    }
    
    • 节流时间戳版  
    function throttle(fn, delay) {
        let last
        return (...args) => {
            let now = +new Date()
            if (!last || now >= last + delay) {
                last = now
                fn.apply(this,args)
            }
        }
    }
    
    • 节流定时器版
    function throttle(fn,wait){
        let timer
        return (...args)=>{
            if(!timer){
                timer = setTimeout(()=>{
                    fn.apply(this,args)
                    clearTimeout(timer)
                },wait)
            }
        }
    }
    

    注:无法确定返回函数在何处调用,所以执行fn函数时对其进行绑定this指向

  • 相关阅读:
    Extension Methods(扩展方法)
    linux面试题
    渗透测试 day4
    渗透测试 day3
    渗透测试 day2
    渗透测试 day1
    9.3 网络安全介绍
    9.2 Iptables
    8.30 进程管理
    8.29 linux的网络
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/12964830.html
Copyright © 2020-2023  润新知