From 25c21f3cca5e853ae9cb68eebd96afc929f7d63b Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Wed, 21 Aug 2024 18:27:43 +0200 Subject: [PATCH] have manual time inputs match timer --- src/components/timer.vue | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components/timer.vue b/src/components/timer.vue index 5b4d091..beca5e9 100644 --- a/src/components/timer.vue +++ b/src/components/timer.vue @@ -9,18 +9,29 @@ } } + const getHours = (seconds) => { + return Math.floor( seconds / 60 / 60 ); + } + const getMinutes = (seconds) => { return Math.floor( seconds / 60 ); } + const createTimeObj = (seconds) => { + const hours = getHours(seconds) + let remainingSeconds = seconds - hours * 60 * 60 + const minutes = getMinutes(remainingSeconds) + remainingSeconds -= minutes * 60 + return { hours, minutes, seconds: remainingSeconds } + } + const getAccumulatedSeconds = (newerTime, olderTime) => { return Math.floor((newerTime-olderTime)/1000); } const formatSeconds = (total) => { - const minutes = getMinutes(total); - const seconds = total - minutes*60; - return `${minutes}:${padDigits(seconds)}`; + const timeObj = createTimeObj(total) + return `${timeObj.hours*60 + timeObj.minutes}:${padDigits(timeObj.seconds)}`; } export default { @@ -98,10 +109,16 @@ } 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) + this.manualHours = timeObj.hours + this.manualMinutes = timeObj.minutes + this.manualSeconds = timeObj.seconds + }, updateSavedPreviousSeconds(event){ this.savedPreviousSeconds = parseInt(event.target.value); }, @@ -163,6 +180,7 @@ this.categories = response.data }) this.totalSeconds = parseInt(window.localStorage.getItem('lastTotalSeconds')) + this.setManualTime(this.totalSeconds) this.savedPreviousSeconds = this.totalSeconds window.onbeforeunload = this.confirmClose },