我的看法:
ns-input#filterName(type="text", v-model="filterName", @keyup="searchTimeOut()")
在我的 vue 代码中:
getUsers() {
.
.
.
API.users.index(params).then(blabla);
.
.
.
},
searchTimeOut() {
let timeout = null;
clearTimeout(timeout);
// Make a new timeout set to go off in 800ms
timeout = setTimeout(() => {
this.getUsers();
console.log("hi")
}, 800);
},
我只想在我停止输入 800 毫秒后调用 getUsers()
一次。现在,我每次写信时都会调用 getUsers()
。
最佳答案
在清除间隔之前删除 this.timer
值。改为这样做:
searchTimeOut() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.timer = setTimeout(() => {
// your code
}, 800);
}
关于javascript - 如何延迟 Vue.js 中的@keyup 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49711449/