I am building a timer and I have set the following as default values in data:
data: () => {
return {
hour: 0;
min: 0;
sec: 0;
}
}
Then I have set the following function to trigger the timer:
methods: {
startTimer: function () {
var self = this;
if (self.hour !== 0 && self.min !== 0 && self.sec !== 0) {
preventDefault();
} else {
this.interval = setInterval(function () {
self.totalSeconds += 1;
self.hour = Math.floor(self.totalSeconds / 3600);
self.min = Math.floor((self.totalSeconds / 60) % 60);
self.sec = parseInt(self.totalSeconds % 60);
}, 1000);
}
},
}
Now, whenever I trigger the timer with a button <div @click="startTimer()">add timer</div> by spamming it, the seconds would jump very fast as if two or three timers running concurrently (which I think it is). I want to set preventDefault() but it doesn't seem to work. Any ideas on how to turn off the button if the hour, mins and seconds are all = 0?