Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 862
WoJ Avatar asked Dec 19 '16 20:12

WoJ


People also ask

What is the input-output analysis table?

The input-output analysis table quantifies the flows of outputs from one industry (in rows) as inputs into another (in columns). In the input-output analysis model, the total economy-wide impact of an economic event can be analyzed from the initial demand change and its direct, indirect, and induced impacts.

How to change the value of an input field using JavaScript?

Changing the value of the input field is no different. There are two approaches to changing the value of an input field using JavaScript. These are: Assigning the value attribute of an element some value using the assignment operator “ = ”

How do I clear the value of an input field?

In this small form, we have an input field with a name of username, and a button with a class of clear. When the user clicks the button, we want to clear the value of the input field. To accomplish this, we first must query for both elements, which we can use querySelector for:

What is environmentally extended input-output analysis (EEIOA)?

Although there are few planning economies nowadays, the input-output analysis does not lose its ground in real-world practices. One is Environmentally Extended Input-Output Analysis (EEIOA), which takes environment-related inputs into account by adding additional columns of inputs such as gasoline and coals.


1 Answers

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/[email protected]"></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.

like image 136
sobolevn Avatar answered Oct 13 '22 10:10

sobolevn