Answer the question
In order to leave comments, you need to log in
How to correctly use a timer as a Vue method (problem when accessing this)?
Dear experts, the situation is as follows: I wrote a code in Vue for a timer that should display a countdown on the screen and change the route at zero.
The problem is that my understanding of the use of this is very limited, and I can't figure out how to reactively change the outer counter of the timer method from the counter function ( f ). Besides, I can't figure out how to make $router work from function f. Perhaps a different approach should be used?
Thank you in advance.
export default {
data() {
return { counter: 10 }
},
methods: {
timer(counter) {
let f = function(counter) {
if (counter>0) {
console.log(counter);
counter--;
setTimeout((f.bind(null,counter)), 1000);
} else (this.$router.push({ path: '/2'}));
}
f(counter);
}
},
mounted() {
this.timer(this.counter);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question