Javascript 如何在Vue.js中延迟@keyup处理程序
我的看法是:Javascript 如何在Vue.js中延迟@keyup处理程序,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我的看法是: ns-input#filterName(type="text", v-model="filterName", @keyup="searchTimeOut()") 在我的vue代码中: getUsers() { . . . API.users.index(params).then(blabla); . . . }, searchTimeOut() { let timeout = null; clearTimeout(timeout
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);
},
我想在我停止键入后只调用一次
getUsers()
,800毫秒。现在,我每次写信都要调用getUsers()
。在清除间隔之前,您先删除这个.timer
值。改为这样做:
searchTimeOut() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.timer = setTimeout(() => {
// your code
}, 800);
}
查看或@emilebergron.lazy如何帮助?
v-model。lazy
将仅在change
事件上触发更改,这些事件在输入失去焦点时触发。也许它在您的特定情况下不起作用,但当您想要延迟v-model
同步时,可能会用到它。嗯,这与问题不太相关。“这并不是一个真正的延迟。@Onondomes乍一看,不清楚这是否是一个搜索建议输入,这就是为什么我链接到了lazy
和debounce问题。但是,.lazy
仍然是一种常见的解决方案,用于在用户完成键入后“延迟”对的API调用。@tomatito有很多通用的解决方案。你可以看看这个线程,我认为你也需要为keydown做一个处理程序。如果我键入一个字符,然后长时间按另一个键,甚至不释放它,会发生什么?