Update 'src/components/timer.vue'

practiced-songs
mahuntington 1 year ago
parent 819decca07
commit 5241492be5

@ -6,16 +6,13 @@
const { proxy } = getCurrentInstance(); const { proxy } = getCurrentInstance();
onMounted(()=>{ onMounted(()=>{
let started = false
window.addEventListener('keydown', (event)=>{ window.addEventListener('keydown', (event)=>{
if(event.key === 'ArrowDown' && !started){ if(event.key === 'ArrowDown'){
event.preventDefault() event.preventDefault()
proxy.start() proxy.start()
started = true } else if (event.key === 'ArrowUp'){
} else if (event.key === 'ArrowUp' && started){
event.preventDefault() event.preventDefault()
proxy.stop() proxy.stop()
started = false
} }
}) })
}) })
@ -78,33 +75,35 @@
}, },
start(event){ start(event){
this.startTime = Date.now(); if(this.running === false){
this.running = true; this.startTime = Date.now();
this.intervalID = setInterval(()=>{ this.running = true;
this.intervalID = setInterval(()=>{
if(this.micThreshold > -1){ if(this.micThreshold > -1){
const array = new Uint8Array(this.analyser.frequencyBinCount); const array = new Uint8Array(this.analyser.frequencyBinCount);
this.analyser.getByteFrequencyData(array); this.analyser.getByteFrequencyData(array);
const arraySum = array.reduce((a, value) => a + value, 0); const arraySum = array.reduce((a, value) => a + value, 0);
this.micLevel = arraySum / array.length; this.micLevel = arraySum / array.length;
if(this.micLevel < this.micThreshold){ if(this.micLevel < this.micThreshold){
this.secondsToSubtract++; this.secondsToSubtract++;
} }
} }
if(this.micLevel >= this.micThreshold){ if(this.micLevel >= this.micThreshold){
this.micThresholdExceeded = true; this.micThresholdExceeded = true;
} else { } else {
this.micThresholdExceeded = false; this.micThresholdExceeded = false;
} }
this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds;
this.setManualTime(this.totalSeconds) this.setManualTime(this.totalSeconds)
window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); window.localStorage.setItem('lastTotalSeconds', this.totalSeconds);
}, 1000); }, 1000);
}
}, },
setManualTime(seconds){ setManualTime(seconds){
const timeObj = createTimeObj(this.totalSeconds) const timeObj = createTimeObj(this.totalSeconds)
@ -117,9 +116,11 @@
this.setManualTime(this.totalSeconds) this.setManualTime(this.totalSeconds)
}, },
stop(event){ stop(event){
this.running = false; if(this.running === true){
this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime); this.running = false;
clearInterval(this.intervalID); this.savedPreviousSeconds += getAccumulatedSeconds(Date.now(), this.startTime);
clearInterval(this.intervalID);
}
}, },
reset(event){ reset(event){
this.savedPreviousSeconds = 0; this.savedPreviousSeconds = 0;

Loading…
Cancel
Save