解决思路:由于在用户不断输入的同时,会不断的触发搜索事件。所以设置一个定时器,延缓搜索的执行,等待用户停止输入的时候再进行查询。
let timer = null
// 搜索框
searchContentChange: function (e) {
let keyWords = e.detail.value ? e.detail.value : null
let that = this
// 清除,定时器
if (timer) {
clearTimeout(timer)
}
// 延迟触发搜索,因为用户有可能不断地键入字母
timer = setTimeout(() => {
if (!keyWords) {
console.error('暂无查询关键词');
} else {
this.searchWords(keyWords)
}
}, 2000)
},