防抖(debounce)
解释:当持续触发某事件时,一定时间间隔内没有再触发事件时,事件处理函数才会执行一次,如果设定的时间间隔到来之前,又一次触发了事件,就重新开始延时。
案例:持续触发scroll事件时,并不立即执行handle函数,当1000毫秒内没有触发scroll事件时,才会延时触发一次handl
e函数。
let timer = null
export default function debounce(fn, delay) {
if (timer) clearTimeout(timer)
timer = setTimeout(()=>{
fn()
}, delay)
}
<input type="text" v-model="val" @input="but" />
methods: {
...mapActions(["search"]),
but() {
debounce(() => {
this.search({ list: list, val: this.val });
}, 1000);
}
}