admin管理员组文章数量:1328778
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: ''
},
puted: {
// a puted getter
len: function() {
// `this` points to the vm instance
return this.message.length
}
}
})
<script src=".1.6/vue.js"></script>
<div id="root">
<input v-model="message">Length: <span>{{len}}</span>
</div>
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: ''
},
puted: {
// a puted getter
len: function() {
// `this` points to the vm instance
return this.message.length
}
}
})
<script src="https://cdnjs.cloudflare./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?
1 Answer
Reset to default 9That works the way it is supposed to. As it is said in the docs:
It will update any bindings that depend on puted 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./ajax/libs/vue/2.1.6/vue.js"></script>
<script src="https://unpkg./[email protected]"></script> <!-- undescore import -->
<div id="root">
<input v-model="message" v-on:keyup="len">Length: <span>{{ messageLength }}</span>
</div>
Full example: https://v2.vuejs/v2/guide/puted.html#Watchers
p.s. A ment about puted
being sync from the vue
's author: https://forum-archive.vuejs/topic/118/throttle-puted-properties/3
p.p.s Classics article about difference between debounce
and throttle
.
本文标签: javascriptHow to temporize the analysis of an ltinputgt fieldStack Overflow
版权声明:本文标题:javascript - How to temporize the analysis of an <input> field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742231744a2437342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论