// 节流 规定一个单位时间,在这个单位时间内,只能有一次触发事件的回调函数执行,如果在同一个单位时间内某事件被触发多次,只有一次能生效。
jieliu() {
// 定时器
//1. 当 timeOut = null 说明我们这时候没有事情出发,是等待状态
let timeOut = this.timeOut;
// 这个时候 timeOut 存在说明我们有事件在执行我们就不在重复执行
if (timeOut) {
return;
}
//2. timeOut= setTimeout(()=>{},1000) timeOut = 14;
this.timeOut = setTimeout(() => {
this.timeOut = null;
// 执行我们想要的函数
this.hGetSuggestion();
}, 3000);
//3. timeOut = 14; timeOut != null 进不去
//4. setTimeout 里面 timeOut = null
//5. 当你设定一个时间之后 ,函数执行的时间短就是这个时间
},
// 防抖 在事件被触发n秒后再执行回调函数,如果在这n秒内又被触发,则重新计时。
fangdou() {
let timeOut = this.timeOut;
if (timeOut) {
// 区别
clearTimeout(this.timeOut);
}
this.timeOut = setTimeout(() => {
// 执行我们想要的函数
this.hGetSuggestion();
}, 3000);
}
}