I'm creating a small timer Vue component. A user needs to be able to start and stop that timer. Here's my component thus far:
<template>
<div>
<a class="u-link-white" href="#" @click="toggleTimer">
{{ time }}
</a>
</div>
</template>
<script>
export default {
props: ['order'],
data() {
return {
time: this.order.time_to_complete,
isRunning: false,
}
},
methods: {
toggleTimer() {
var interval = setInterval(this.incrementTime, 1000);
if (this.isRunning) {
//debugger
clearInterval(interval);
console.log('timer stops');
} else {
console.log('timer starts');
}
this.isRunning = (this.isRunning ? false : true);
},
incrementTime() {
this.time = parseInt(this.time) + 1;
},
}
}
</script>
I'm toggling the isRunning
variable to determine whether the timer is running or not. On first click (the play), the timer begins and increments successfully.
However, on the second click (the pause), the isRunning
var toggles back to off, but clearInterval(this.incrementTime)
is not clearing the interval and pausing the timer. When I insert that debugger, and manually hit clearInterval(interval)
via the console, it returns undefined.
Does anybody have any insight on how I've formatted my component incorrectly?