为了优化性能,避免短时间重复请求
新建util.js文件
// 防抖函数
let debounce = function (func, delay){
let timer;
return function(){
let _this = this;
let args = arguments;
if(timer) clearTimeout(timer);
timer = setTimeout(()=>{
func.apply(_this,args);
},delay);
}
}
// 节流函数
let throttle = function(func, delay){
let timeout;
return function(){
let _this = this;
let args = arguments;
if(!timeout){
timeout = setTimeout(()=>{
timeout = null;
func.apply(_this, args);
},delay)
}
}
}
export { debounce, throttle }
页面引用util.js
import { debounce } from '../../js/util.js'
searchInput: debounce(function(e){
var value = e.detail.value
if (value) {
this.setData({
inputValue: value
});
this.getSearchList();
}
},500),