fixed intervals to timeouts

This commit is contained in:
2026-01-20 22:44:30 +00:00
parent 25dc5e0a97
commit c25e9263fd
2 changed files with 15 additions and 10 deletions

View File

@@ -14,22 +14,24 @@ const currentIndex = ref(0);
const currentComment = computed(() => images[currentIndex.value].comment); const currentComment = computed(() => images[currentIndex.value].comment);
const currentUrl = computed(() => images[currentIndex.value].url); const currentUrl = computed(() => images[currentIndex.value].url);
let nextId;
function nextImage() { function nextImage() {
clearTimeout(nextId);
let newIndex; let newIndex;
do { do {
newIndex = Math.floor(Math.random() * images.length); newIndex = Math.floor(Math.random() * images.length);
} while (newIndex === currentIndex.value); // prevent same image repeating } while (newIndex === currentIndex.value);
currentIndex.value = newIndex; currentIndex.value = newIndex;
nextId = setTimeout(nextImage, 10000);
} }
let intervalId;
onMounted(() => { onMounted(() => {
intervalId = setInterval(nextImage, 10000); nextId = setTimeout(nextImage, 10000);
}); });
onUnmounted(() => { onUnmounted(() => {
clearInterval(intervalId); clearTimeout(nextId);
}); });
</script> </script>

View File

@@ -2,7 +2,7 @@
import { ref, computed, onMounted, onUnmounted } from "vue"; import { ref, computed, onMounted, onUnmounted } from "vue";
import axios from "axios"; import axios from "axios";
let intervalId = null; let nextId = null;
let refreshId = null; let refreshId = null;
const song = computed(() => songs.value[idx.value]); const song = computed(() => songs.value[idx.value]);
@@ -31,22 +31,25 @@ async function fetchRecent() {
} catch (err) { } catch (err) {
console.error("Cannot connect to Spotify API", err); console.error("Cannot connect to Spotify API", err);
} }
refreshId = setTimeout(fetchRecent, 120000);
} }
function nextSong() { function nextSong() {
clearTimeout(nextId);
if (!songs.value.length) return; if (!songs.value.length) return;
idx.value = (idx.value + 1) % songs.value.length; idx.value = (idx.value + 1) % songs.value.length;
nextId = setTimeout(nextSong, 5000);
} }
onMounted(async () => { onMounted(async () => {
await fetchRecent(); await fetchRecent();
intervalId = setInterval(nextSong, 5000); nextId = setTimeout(nextSong, 5000);
refreshId = setInterval(fetchRecent, 120000); refreshId = setTimeout(fetchRecent, 120000);
}); });
onUnmounted(() => { onUnmounted(() => {
clearInterval(intervalId); clearTimeout(nextId);
clearInterval(refreshId); clearTimeout(refreshId);
}); });
</script> </script>