How to implement debounce in Vue2?

前端 未结 13 595
梦谈多话
梦谈多话 2020-11-27 11:08

I have a simple input box in a Vue template and I would like to use debounce more or less like this:



        
相关标签:
13条回答
  • 2020-11-27 11:26

    Although pretty much all answers here are already correct, if anyone is in search of a quick solution I have a directive for this. https://www.npmjs.com/package/vue-lazy-input

    It applies to @input and v-model, supports custom components and DOM elements, debounce and throttle.

    Vue.use(VueLazyInput)
      new Vue({
        el: '#app', 
        data() {
          return {
            val: 42
          }
        },
        methods:{
          onLazyInput(e){
            console.log(e.target.value)
          }
        }
      })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <script src="https://unpkg.com/lodash/lodash.min.js"></script><!-- dependency -->
    <script src="https://unpkg.com/vue-lazy-input@latest"></script> 
    
    <div id="app">
      <input type="range" v-model="val" @input="onLazyInput" v-lazy-input /> {{val}}
    </div>

    0 讨论(0)
  • 2020-11-27 11:29

    If you need a very minimalistic approach to this, I made one (originally forked from vuejs-tips to also support IE) which is available here: https://www.npmjs.com/package/v-debounce

    Usage:

    <input v-model.lazy="term" v-debounce="delay" placeholder="Search for something" />
    

    Then in your component:

    <script>
    export default {
      name: 'example',
      data () {
        return {
          delay: 1000,
          term: '',
        }
      },
      watch: {
        term () {
          // Do something with search term after it debounced
          console.log(`Search term changed to ${this.term}`)
        }
      },
      directives: {
        debounce
      }
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 11:31

    I had the same problem and here is a solution that works without plugins.

    Since <input v-model="xxxx"> is exactly the same as

    <input
       v-bind:value="xxxx"
       v-on:input="xxxx = $event.target.value"
    >
    

    (source)

    I figured I could set a debounce function on the assigning of xxxx in xxxx = $event.target.value

    like this

    <input
       v-bind:value="xxxx"
       v-on:input="debounceSearch($event.target.value)"
    >
    

    methods:

    debounceSearch(val){
      if(search_timeout) clearTimeout(search_timeout);
      var that=this;
      search_timeout = setTimeout(function() {
        that.xxxx = val; 
      }, 400);
    },
    
    0 讨论(0)
  • 2020-11-27 11:32
     public debChannel = debounce((key) => this.remoteMethodChannelName(key), 200)
    

    vue-property-decorator

    0 讨论(0)
  • 2020-11-27 11:37

    I am using debounce NPM package and implemented like this:

    <input @input="debounceInput">
    
    methods: {
        debounceInput: debounce(function (e) {
          this.$store.dispatch('updateInput', e.target.value)
        }, config.debouncers.default)
    }
    

    Using lodash and the example in the question, the implementation looks like this:

    <input v-on:input="debounceInput">
    
    methods: {
      debounceInput: _.debounce(function (e) {
        this.filterKey = e.target.value;
      }, 500)
    }
    
    0 讨论(0)
  • 2020-11-27 11:39

    We can do with by using few lines of JS code:

    if(typeof window.LIT !== 'undefined') {
          clearTimeout(window.LIT);
    }
    
    window.LIT = setTimeout(() => this.updateTable(), 1000);
    

    Simple solution! Work Perfect! Hope, will be helpful for you guys.

    0 讨论(0)
提交回复
热议问题