javascript - 如何延迟 Vue.js 中的@keyup 处理程序

标签 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);
  // 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/

相关文章:

vue.js - 如何使用 v-model 代替 v-bind?

vue.js - 为什么 beforeRouteLeave 从未被调用?

javascript - 传递将在子组件中的样式中使用的 Prop - Vue

javascript - Axios VueJs 响应映射独特

javascript - 如何找到数组中具有特定值的对象的索引?

php - WebSocket 消息前面的 [] 字符?

javascript - 用作对象时数据不会更新,但作为变量时会正常更改

javascript - 瘦客户端和胖客户端之间的通信

javascript - 具有多个属性的绘制元素

javascript - 如何在 Vue 中的渲染列表中打开模式?