How to temporize the analysis of an <input> field?

后端 未结 1 707
春和景丽
春和景丽 2021-01-12 13:12

I would like to analyze the content of an field when there is no user activity.

I will take below a simple example (counting the number o

相关标签:
1条回答
  • 2021-01-12 13:35

    That works the way it is supposed to. As it is said in the docs:

    It will update any bindings that depend on computed property when the original data changes

    But there's a way to do it:

    var app = new Vue({
      el: '#root',
      data: {
        message: '',
        messageLength:  0
      },
      methods: {
        len: _.debounce(
          function() {
            this.messageLength = this.message.length
          }, 
          300 // time
        )
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.js"></script>
    <script src="https://unpkg.com/underscore@1.8.3"></script> <!-- undescore import -->
    <div id="root">
      <input v-model="message" v-on:keyup="len">Length: <span>{{ messageLength }}</span>
    </div>

    Full example: https://vuejs.org/v2/guide/computed.html#Watchers

    p.s. A comment about computed being sync from the vue's author: https://forum-archive.vuejs.org/topic/118/throttle-computed-properties/3

    p.p.s Classics article about difference between debounce and throttle.

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