Skip to content

Commit

Permalink
feat: join strat/pause button
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrSl committed Jan 15, 2025
1 parent 20c6edb commit 7471b17
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/components/Timer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const { steps } = Astro.props
</div>
<div class="control-panel">
<button class="control-panel--btn" id="startTimer">Start</button>
<button class="control-panel--btn" id="stopTimer">Pause</button>
<button class="control-panel--btn" id="stopBrew">Reset</button>
</div>
</div>
Expand Down Expand Up @@ -138,6 +137,9 @@ const { steps } = Astro.props
.control-panel--btn {
font-size: 16px;
padding: 8px 14px;
border-radius: 4px;
border: none;
min-width: 100px;
}
</style>

Expand All @@ -160,6 +162,7 @@ const { steps } = Astro.props
const totalWeightSlot = document.getElementById('totalWeight')
const nextStepSlot = document.getElementById('nextStep')
const progressArc = document.getElementById('progressArc')
const startTimerBtn = document.getElementById('startTimer')!

const steps: Step[] = root?.dataset.steps
? JSON.parse(root.dataset.steps).map((it: Step) => ({
Expand Down Expand Up @@ -191,14 +194,24 @@ const { steps } = Astro.props
progressArc.setAttribute('stroke-dasharray', getDashArray((displayTime / step.time) * 100))
}

function toggleTimer() {
if (isRunning) {
stopTimer()
} else {
startTimer()
}
}

function startTimer() {
isRunning = true
startTime = performance.now() - elapsedTime
requestAnimationFrame(updateTimer)
startTimerBtn.textContent = 'Pause'
}

function stopTimer() {
isRunning = false
startTimerBtn.textContent = 'Start'
}

function resetTimer() {
Expand Down Expand Up @@ -229,10 +242,8 @@ const { steps } = Astro.props
const totalMilliseconds = Math.floor(time)
const minutes = Math.floor((totalMilliseconds % 3600_000) / 60_000)
const seconds = Math.floor((totalMilliseconds % 60_000) / 1000)
// const milliseconds = totalMilliseconds % 1000;

return pad(minutes) + ':' + pad(seconds)
// + ":" + milliseconds.toString().padStart(3, "0");
}

function getDashArray(percent: number) {
Expand All @@ -250,19 +261,12 @@ const { steps } = Astro.props
resetTimer()
}

const startTimerBtn = document.getElementById('startTimer')
const stopTimerBtn = document.getElementById('stopTimer')
const stopBrewBtn = document.getElementById('stopBrew')

if (startTimerBtn && stopBrewBtn && stopTimerBtn) {
if (startTimerBtn && stopBrewBtn) {
startTimerBtn.addEventListener('click', () => {
startTimer()
toggleTimer()
})

stopTimerBtn.addEventListener('click', () => {
stopTimer()
})

stopBrewBtn.addEventListener('click', () => {
stopBrew()
})
Expand Down

0 comments on commit 7471b17

Please sign in to comment.