Update 'src/components/timer.vue'

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

@ -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;

Loading…
Cancel
Save