如何在Vue.js中延迟@keyup处理程序

tom*_*ito 6 javascript vue.js vuejs2

我的看法:

ns-input#filterName(type="text", v-model="filterName", @keyup="searchTimeOut()")
Run Code Online (Sandbox Code Playgroud)

在我的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);
},
Run Code Online (Sandbox Code Playgroud)

getUsers()在我停止输入和800毫秒后我只想打电话一次.现在,我getUsers()每次写信都打电话.

oni*_*mes 16

this.timer在清除间隔之前删除值.改为:

searchTimeOut() {  
    if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
    }
    this.timer = setTimeout(() => {
        // your code
    }, 800);
}
Run Code Online (Sandbox Code Playgroud)