diff --git a/vue/src/components/util/Timer.vue b/vue/src/components/util/Timer.vue index f92ee0e..1e52a1f 100644 --- a/vue/src/components/util/Timer.vue +++ b/vue/src/components/util/Timer.vue @@ -12,8 +12,35 @@ import Header from "@/components/text/Header.vue"; import { ref, onUnmounted } from "vue"; +/** Which widget is showing — countdown timer or stopwatch. */ +const tab = ref("timer"); const timer = ref(null); +// --- Stopwatch --- +// Same interval-tick pattern as the countdown, but counts up with no target. +const stopwatchInterval = ref(null); +const stopwatchRunning = ref(false); +const stopwatchMs = ref(0); + +function stopwatchTick() { + stopwatchMs.value += 1000; +} + +function toggleStopwatch() { + stopwatchRunning.value = !stopwatchRunning.value; + if (stopwatchRunning.value) { + stopwatchInterval.value = setInterval(stopwatchTick, 1000); + } else { + clearInterval(stopwatchInterval.value); + } +} + +function resetStopwatch() { + clearInterval(stopwatchInterval.value); + stopwatchRunning.value = false; + stopwatchMs.value = 0; +} + /** "idle" | "running" | "paused" | "finished" */ const state = ref("idle"); @@ -69,6 +96,7 @@ function resetTimer() { onUnmounted(() => { clearInterval(timer.value); + clearInterval(stopwatchInterval.value); }); function playFinishedSound() { @@ -79,7 +107,24 @@ function playFinishedSound() {