I define the timer in each my-progress, used to update the value of view, but the console shows the value of the constant changes, and the value of view is still not changed, how can I do in the timer to change the value of view
Vue.component('my-progress', { template: '\ <div class="progress progress-bar-vertical" data-toggle="tooltip" data-placement="top">\ <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" :style="{height: pgvalue}">{{pgvalue}}\ </div>\ </div>\ ', data : function(){ return { pgvalue : '50%', intervalid1:'', } }, computed:{ changes : { get : function(){ return this.pgvalue; }, set : function(v){ this.pgvalue = v; } } }, mounted : function(){ this.todo() }, beforeDestroy () { clearInterval(this.intervalid1) }, methods : { todo : function(){ this.intervalid1 = setInterval(function(){ this.changes = ((Math.random() * 100).toFixed(2))+'%'; console.log (this.changes); }, 3000); } }, })
here is the link: jsbin.com/safolom
So inorder to use the setInterval method you have to make this change: methods: { startInterval: function () { setInterval ( () => { this.myText = myOuterText + 1 }, 1000); } } Since Vue is a JavaScript framework so it makes sense to have some proper knowledge of JavaScript like setInterval, arrow method, etc.
The problem is that because the function you’re passing to setInterval is not an arrow function, this.mytext actually refers to window.myText and not your components myText. And how do I reference component variants? Use an outer scope or closure variable.
It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component. Vue also plays nicely with native Web Components.
I came up with an easy way to repeat a function in Vue.js. For instance, you want to periodically retrieve data from your database using the backend API/axios, without any additional packages. To achieve that you need to use the javascript method called setInterval (). It’s a simple function that would repeat the same task over and over again.
this
is not pointing to the Vue. Try
todo: function(){ this.intervalid1 = setInterval(function(){ this.changes = ((Math.random() * 100).toFixed(2))+'%'; console.log (this.changes); }.bind(this), 3000); }
or
todo: function(){ const self = this; this.intervalid1 = setInterval(function(){ self.changes = ((Math.random() * 100).toFixed(2))+'%'; console.log (this.changes); }, 3000); }
or
todo: function(){ this.intervalid1 = setInterval(() => { this.changes = ((Math.random() * 100).toFixed(2))+'%'; console.log (this.changes); }, 3000); }
See How to access the correct this
inside a callback?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With