display time as it changes. allow multiple timer starts and keep track of accumulated seconds

mic_level
Matthew Huntington 3 years ago
parent 9a1f15e5f4
commit ade7fa9612

@ -11,7 +11,8 @@
categories:[], categories:[],
running: false, running: false,
startTime:0, startTime:0,
savedPreviousSeconds:0 savedPreviousSeconds:0,
totalSeconds:0
} }
}, },
methods: { methods: {
@ -25,13 +26,18 @@
start(event){ start(event){
this.startTime = Date.now(); this.startTime = Date.now();
this.running = true; this.running = true;
this.intervalID = setInterval(()=>{
this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) + this.savedPreviousSeconds;
}, 1000);
}, },
stop(event){ stop(event){
this.running = false; this.running = false;
this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime); this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime);
clearInterval(this.intervalID);
}, },
reset(event){ reset(event){
this.savedPreviousSeconds = 0; this.savedPreviousSeconds = 0;
this.totalSeconds = 0;
} }
}, },
mounted() { mounted() {
@ -47,13 +53,13 @@
<summary><h2>Timer</h2></summary> <summary><h2>Timer</h2></summary>
<button :disabled="running" @click="start">Start Timer</button> <button :disabled="running" @click="start">Start Timer</button>
<button :disabled="!running" @click="stop">Stop Timer</button> <button :disabled="!running" @click="stop">Stop Timer</button>
<button :disabled="running || savedPreviousSeconds === 0" @click="reset">Reset</button> <button :disabled="running || totalSeconds === 0" @click="reset">Reset</button>
<form @submit="submit"> <form @submit="submit">
<label>Description</label> <label>Description</label>
<input v-model="description" type="text"/> <input v-model="description" type="text"/>
<label>Seconds</label> <label>Seconds</label>
<input v-model="savedPreviousSeconds" type="number"/> <input v-model="totalSeconds" type="number"/>
<label>Comments</label> <label>Comments</label>
<textarea v-model="comments"/> <textarea v-model="comments"/>

Loading…
Cancel
Save