// 防抖函数
export function debounce(func, delay) {
let timer //定义一个变量接收参数
return (...args) => {
if (timer) {
clearTimeout(timer) //每次进入先清一次定时器,防止上次的定时器还在执行
}
timer = setTimeout(() => {
func.apply(this, args) //更改this指向
}, delay)
}
}
// 联想搜索
this.$watch('searchinput', debounce((newQuery) => {
if(newQuery) {
this.fnSearch(newQuery)
}
}, 500), { immediate: false })