最近在做一个内容搜索的需求,要求是每次输入字符都要显示搜索结果,但是完成之后调试发现当我连续输入字符的时候每输入一次字符都发送了一次请求,这样大大的占用了网络资源,所以要对我们的输入过程进行节流。
code:
export function lowerRequest(func, delay) {
let time;
return function (...args) {
if (time) {
clearTimeout(time);
}
time = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
然后在我们输入框改变引起发送请求的地方使用就可以啦,简单易懂,haha….