首页 > 其他 > 详细

防抖函数的运用

时间:2019-07-13 16:18:22      阅读:89      评论:0      收藏:0      [点我收藏+]

发过程中有要求做一个搜索框,在input输入框输入了文字之后就调一次接口,当用户输入过快就会疯狂调接口,很多接口返回数据后台并不是有用,因此使用了防抖函数。代码如下

函数debounce
export function debounce(fn, delay) {
let delays = delay || 500;
let timer;
return function () {
let th = this;
let args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
timer = null;
fn.apply(th, args);
}, delays);
};
}
使用
<input type="text" v-model="memberName" @input="onSearch">
 
import { debounce } from "@/common/js/debounce.js";
 
onSearch: debounce(function() {
//节流
let params = {
};
getProjectManagerList(params).then(res => {
let data = res.data.....
}
});
}, 1000),

防抖函数的运用

原文:https://www.cnblogs.com/dmwcq/p/11180842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!