I am facing a stack overflow error while creating a timer using Vue, and I'm struggling to understand the root cause. Can someone provide insights on the following code:
Here is my template structure:
<span class="coundown-number"> {{ minutesRemaining }} : {{ secondsRemaining }}</span>
<span v-if="countdownDone"&g DONE </span>
<button v-if="running" class="custom-button" type="button" @click="runCountdown(countdownTime)">Start</button>
<input class="set-time" type="number" v-model="countdownTime" placeholder="Enter a Time in Minutes">
This is the implementation in my JavaScript file:
export default {
name: 'Countdown',
props: {
},
data() {
return {
running: true,
countdownTime: null,
remainingTime: null,
countdownDone: false,
}
},
computed: {
minutesRemaining() {
return (this.remainingTime - (this.remainingTime % 60)) / 60;
},
secondsRemaining() {
return this.remainingTime % 60;
},
},
methods: {
runCountdown(timerTime) {
let currentTime = timerTime*60;
this.remainingTime = currentTime;
if (currentTime > 0){
currentTime--;
setInterval(this.runCountdown(this.currentTime), 1000);
}
else{
this.countdownDone = true;
}
}
}
}
I suspect the issue lies with how I'm calling the setInterval
function within the same method it's being created. However, I'm unsure of an alternative approach for initialization.
If it were defined externally as
let tick = setInterval(runCountdown, 1000)
, how can I trigger it onclick? Any guidance on resolving this issue and understanding the underlying concept would be greatly appreciated.