防抖函数
定义防抖函数
// 防抖函数 constants.js
export const delay = (function () {
let timer = null
return function (callback, ms) {
clearTimeout(timer)
timer = setTimeout(callback, ms)
}
})()
// 或者这样调用
searchInfo (query) {
this.timer && clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.getNodeList(query)
}, 200)
}
使用防抖函数
// xxx.vue
methods: {
// 监听input框键盘按下就调用
searchDelay () {
delay(() => {
this.searchNode()
}, 500)
}
}