I am attempting to create a countdown timer in Vue.js that displays the remaining time.
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!"
}
},
However, I am encountering an issue where the message2
does not update with each tick of the Interval when the countDown()
function is called. Instead, it only runs once after the initial execution.