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?
That works the way it is supposed to. As it is said in the docs:
But there's a way to do it:
Full example: https://vuejs.org/v2/guide/computed.html#Watchers
p.s. A comment about
computed
being sync from thevue
's author: https://forum-archive.vuejs.org/topic/118/throttle-computed-properties/3p.p.s Classics article about difference between
debounce
andthrottle
.