I'm trying to build a timer using Vue.js and I want it to output the time left.
new Vue({
el: "#app",
data: {
timer: 25,
message2: "",
counter: false,
interval: null
},
methods: {
startTimer() {
this.interval = setInterval(this.countDown(), 1000);
},
countDown() {
if (this.counter === false) {
var n = this.timer;
this.counter = true;
} else if (n > 0) {
n -= 1;
this.message2 = "You have " + n + "seconds left.";
} else {
clearInterval(this.interval);
this.counter = false;
this.message2 = "Your time is up!"
}
},
But I cannot get the message2
updated every time Interval runs the function countDown()
. Actually it doesn't run the function every second but only once.