Skip to content

Commit

Permalink
Swap setInterval for setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
hvangeffen committed Jan 19, 2024
1 parent f49675c commit e6e085f
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/components/general/DateTimeSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@
<div class="play-controls">
<v-menu offset="25" transition="fade-transition">
<template v-slot:activator="{ props }">
<v-btn
v-bind="props"
density="compact"
variant="text"
icon
>
<v-btn v-bind="props" density="compact" variant="text" icon>
<v-icon>mdi-play-speed</v-icon>
<v-tooltip location="top" activator="parent">
<span>Playback speed</span>
Expand Down Expand Up @@ -136,7 +131,7 @@ const currentSpeed = ref(defaultSpeed)
const availableSpeeds = [0.5, 1, 2, 4]
const isPlaying = ref(false)
let playIntervalTimer: ReturnType<typeof setInterval> | null = null
let playTimeoutTimer: ReturnType<typeof setTimeout> | null = null
const doFollowNow = ref(props.doFollowNow)
let followNowIntervalTimer: ReturnType<typeof setInterval> | null = null
Expand Down Expand Up @@ -279,17 +274,14 @@ function togglePlay(): void {
function startPlay(): void {
isPlaying.value = true
stopFollowNow()
playIntervalTimer = setInterval(
play,
props.playInterval * (1 / currentSpeed.value),
)
play()
}
function stopPlay(): void {
isPlaying.value = false
if (playIntervalTimer) {
clearInterval(playIntervalTimer)
playIntervalTimer = null
if (playTimeoutTimer) {
clearTimeout(playTimeoutTimer)
playTimeoutTimer = null
}
}
Expand All @@ -298,6 +290,12 @@ function play(): void {
if (dateIndex.value === maxIndex.value) {
stopPlay()
}
if (isPlaying.value) {
playTimeoutTimer = setTimeout(
play,
props.playInterval * (1 / currentSpeed.value),
)
}
}
function stepBackward(): void {
Expand All @@ -320,10 +318,6 @@ function increment(step: number): void {
function setSpeed(speed: number) {
currentSpeed.value = speed
if (isPlaying.value) {
stopPlay()
startPlay()
}
}
function formatSpeed(speed: number) {
Expand Down

0 comments on commit e6e085f

Please sign in to comment.