7

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

I will take below a simple example (counting the number of characters) but the actual analysis if very expensive so I would like to do it in batches, when there is some inactivity of the user instead of doing it at every change of the bound variable.

The code for the straightforward analysis could be

var app = new Vue({
  el: '#root',
  data: {
    message: ''
  },
  computed: {
    // a computed getter
    len: function() {
      // `this` points to the vm instance
      return this.message.length
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.js"></script>
<div id="root">
  <input v-model="message">Length: <span>{{len}}</span>
</div>

My problem is that function() is called at each change of message. Is there a built-in mechanism to throttle the query, or a typical approach to such a problem in JS?

WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

9

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://v2.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.

tony19
  • 125,647
  • 18
  • 229
  • 307
sobolevn
  • 16,714
  • 6
  • 62
  • 60
  • I believe a `debounce` would be more appropriate here. Still a +1 from me, though. – Bill Criswell Dec 19 '16 at 22:49
  • Thanks, added the link to the appropriate article. – sobolevn Dec 19 '16 at 22:56
  • I came across this link just moments ago to make sure I was thinking about it as well: http://demo.nimius.net/debounce_throttle/ – Bill Criswell Dec 19 '16 at 22:57
  • The `p.p.s`'s link is missing :/ . And [another article](https://css-tricks.com/debouncing-throttling-explained-examples/) from [the lodash doc](https://lodash.com/docs/4.17.2#debounce). – JJPandari Dec 20 '16 at 07:55
  • @sobolevn: thank you. Would you know if this approach can actually make the computation after some activity? The example you gave seems to stop the update while there is activity but not perform it afterwards. In other words, one needs to press one more key to have the result displayed (and the result is for the string without that last character) – WoJ Dec 20 '16 at 08:10
  • 1
    @sobolevn: After some tests I have actually asked that in a [separate question](http://stackoverflow.com/questions/41238872/is-vue-js-and-debounce-lodash-underscore-compatible) – WoJ Dec 20 '16 at 09:29