vue 搜索框实现防抖调接口
- 首先在html中输入一个input框
<el-input v-model="search" @input="inputSearch()" ></el-input>
- 在 js 中写入下面方法
inputSearch(status) {
this.debounce(this.debounceInputSearch, 1000)
},
debounce(func, wait = 500, immediate = false) {
if (this.timeout !== null) clearTimeout(this.timeout)
if (immediate) {
var callNow = !this.timeout
this.timeout = setTimeout(function() {
this.timeout = null
}, wait)
if (callNow) typeof func === 'function' && func.apply(this, arguments)
} else {
this.timeout = setTimeout(() => {
typeof func === 'function' && func.apply(this, arguments)
}, wait)
}
},
async debounceInputSearch() {
await getVisual()
},