From 5241492be5d3b3a18d47108a623882c4bd1b2f1a Mon Sep 17 00:00:00 2001 From: mahuntington Date: Mon, 21 Oct 2024 16:59:02 -0400 Subject: [PATCH] Update 'src/components/timer.vue' --- src/components/timer.vue | 59 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/components/timer.vue b/src/components/timer.vue index 8fe91dd..12e9590 100644 --- a/src/components/timer.vue +++ b/src/components/timer.vue @@ -6,16 +6,13 @@ const { proxy } = getCurrentInstance(); onMounted(()=>{ - let started = false window.addEventListener('keydown', (event)=>{ - if(event.key === 'ArrowDown' && !started){ + if(event.key === 'ArrowDown'){ event.preventDefault() proxy.start() - started = true - } else if (event.key === 'ArrowUp' && started){ + } else if (event.key === 'ArrowUp'){ event.preventDefault() proxy.stop() - started = false } }) }) @@ -78,33 +75,35 @@ }, start(event){ - this.startTime = Date.now(); - this.running = true; - this.intervalID = setInterval(()=>{ + if(this.running === false){ + this.startTime = Date.now(); + this.running = true; + this.intervalID = setInterval(()=>{ - if(this.micThreshold > -1){ + if(this.micThreshold > -1){ - const array = new Uint8Array(this.analyser.frequencyBinCount); - this.analyser.getByteFrequencyData(array); - const arraySum = array.reduce((a, value) => a + value, 0); - this.micLevel = arraySum / array.length; + const array = new Uint8Array(this.analyser.frequencyBinCount); + this.analyser.getByteFrequencyData(array); + const arraySum = array.reduce((a, value) => a + value, 0); + this.micLevel = arraySum / array.length; - if(this.micLevel < this.micThreshold){ - this.secondsToSubtract++; - } + if(this.micLevel < this.micThreshold){ + this.secondsToSubtract++; + } - } + } - if(this.micLevel >= this.micThreshold){ - this.micThresholdExceeded = true; - } else { - this.micThresholdExceeded = false; - } + if(this.micLevel >= this.micThreshold){ + this.micThresholdExceeded = true; + } else { + this.micThresholdExceeded = false; + } - this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; - this.setManualTime(this.totalSeconds) - window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); - }, 1000); + this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; + this.setManualTime(this.totalSeconds) + window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); + }, 1000); + } }, setManualTime(seconds){ const timeObj = createTimeObj(this.totalSeconds) @@ -117,9 +116,11 @@ this.setManualTime(this.totalSeconds) }, stop(event){ - this.running = false; - this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime); - clearInterval(this.intervalID); + if(this.running === true){ + this.running = false; + this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime); + clearInterval(this.intervalID); + } }, reset(event){ this.savedPreviousSeconds = 0;