缺点是不能完全封装

含义:fn立即执行一次,过了一段时间后,才允许再次执行

switch=true
execTime:number
throttle(fn,interval){
    if(new Date().getTime()-this.execTime>interval){this.switch=true}
    if(this.switch){
        fn();
        this.switch=false;
        this.execTime=new Date().getTime();
    }
    
}

二 去抖动 debounce

1.含义:去抖动函数不断执行,直到去抖动函数停止执行多少毫秒后,受保护的函数才执行

eg:

this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
this.debouncedGetAnswer()
//debouncedGetAnswe:去抖动函数
//this.getAnswer:受保护的函数

2.适用场景

(1)键盘输入:用户不断输入,如果暂定时间超过多少毫秒,就接受用户的输入


努力求学的人
108 声望2 粉丝