美文网首页
input输入框 使用 防抖

input输入框 使用 防抖

作者: IssunRadiance | 来源:发表于2023-04-18 16:26 被阅读0次

防抖

<el-input v-model="value" @input="inputChange"></el-input>
<script>
export default {
  data () {
    return {
      value: '',
      timer: null
    }
  },
  methods: {
    inputChange() {
      if (this.timer !== null) clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        console.log(this.value)
      }, 1000)
    }
  }
}
</script>

节流

throttle() {
  if (this.flag) {
    setTimeout(() => {
      console.log('value')
      this.flag = true
    }, 1000)
  }
  this.flag = false
}
附加
// 防抖 -- 相当于“回城”,每次都重新计时
// 节流 -- 相当于“技能”,冷却结束后,才可继续执行

相关文章

网友评论

      本文标题:input输入框 使用 防抖

      本文链接:https://www.haomeiwen.com/subject/yeahjdtx.html