缺点是不能完全封装
含义: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)键盘输入:用户不断输入,如果暂定时间超过多少毫秒,就接受用户的输入
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。