莫走往下看
//模仿ajax请求
ajax = (content) => {
console.log('ajax:' + content)
};
onDebounceKeyUpClick = (e) => {//加入防抖动后 在频繁输入后 不会发送请求
let debounceAjax = this.debounce(() => this.ajax(e.target.value), 10000)
debounceAjax(e);
};
debounce = (fn, delay = 10000) => {
//期间间隔执行 节流
return (rest) => { //箭头函数是没有arguments的 所以用...rest 来代替
let args = rest;
args.persist();
if (this.state.timerId) clearTimeout(this.state.timerId);//要用this.timerId 而不能直接定义var timerId=null;
this.setState({
timerId: setTimeout(() => {
fn.call(this, args);
}, delay)
});
};
};
<Search
placeholder="姓名/手机号"
onSearch={() => this.onSearch()}.
onChange={(e) => this.onDebounceKeyUpClick(e)}
/>
说到最后希望这篇帖子能够帮助到你,代码可以抄但是更得理解,多敲才是王道希望你对代码有着真挚的感情,对自己敲出的代码都有不一样的自信,感谢您的浏览