在短期屡次触发某个函数的场景下,对执行函数进行节流,节省无效浪费javascript
1. 使用场景php
export function debounce(fun, delay){
let timer //定时器
return function (...args){
if (timer) {
clearTimeout(timer)
}
timer = setTimerout(() => { //
fun.apply(this, args)
}, delay)
}
}
复制代码
注:代码来源vue-music音乐播放器项目vue
2. 函数调用java
this.$watch(
"query",
debounce(newQuery => {
// 不超过200ms函数节流
this.$emit("query", newQuery);
}, 200)
);
复制代码
this.$emit("query", newQuery);
前给函数加个节流函数3. 误区git
博客原文地址
项目地址:vue-music音乐播放器项目github