Currently experimenting with VueJS and managed to develop a basic counter. I'm attempting to reset the setInterval()
method using the resetTimer()
function, but it's not functioning as expected. Unsure of what steps I might be overlooking.
new Vue({
el: '#app',
data: {
hours: 0,
minutes: 0,
seconds: 0,
counter: 0
},
methods: {
timer() {
setInterval(() => {
this.seconds = this.timerFormat(++this.counter % 60)
this.minutes = this.timerFormat(parseInt(this.counter / 60, 10) % 60)
this.hours = this.timerFormat(parseInt(this.counter / 3600, 10))
}, 1000);
},
resetTimer() {
clearInterval(this.timer())
},
timerFormat(timer) {
return timer > 9 ? timer : '0' + timer;
}
}
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning Vue</title>
</head>
<body>
<div id="app">
<p>{{ hours }} : {{ minutes }} : {{ seconds }}</p>
<button @click="timer">Start</button>
<button @click="resetTimer">Reset</button>
</div>
<script src="dist/bundle.js"></script>
</body>
</html>