Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  67] [ 6]  / answers: 1 / hits: 133084  / 7 Years ago, tue, april 11, 2017, 12:00:00

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


More From » vuejs2

 Answers
23

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?


[#58197] Saturday, April 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;