From 2bdbc58b1f604ceb432d83bf65a7e084fd4a9adf Mon Sep 17 00:00:00 2001 From: Digital Yeti Date: Mon, 3 Mar 2025 14:59:46 -0600 Subject: [PATCH] Add proper slide timer functionality to slideshowpure.js This is a fix for #14 --- slideshowpure.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/slideshowpure.js b/slideshowpure.js index d68d5ac..ea8ae97 100644 --- a/slideshowpure.js +++ b/slideshowpure.js @@ -403,6 +403,15 @@ const slidesInit = async () => { }); }; const initializeSlideshow = () => { + var slideInterval = null; + class SlideTimer { + constructor(fnctn, time) { + var timerObject = setInterval(fnctn, time); + this.stop = function () { if (timerObject) { clearInterval(timerObject); timerObject = null; } return this; }; + this.start = function () { if (!timerObject) { this.stop(); timerObject = setInterval(fnctn, time); } return this; }; + this.restart = function () { return this.stop().start(); }; + } + } const slides = document.querySelectorAll(".slide"); createPaginationDots(); const container = document.getElementById("slides-container"); @@ -414,6 +423,7 @@ const slidesInit = async () => { isTransitioning = !0; currentSlideIndex = (index + slides.length) % slides.length; showSlide(currentSlideIndex); + slideInterval.restart(); setTimeout(() => { isTransitioning = !1; }, 500); @@ -426,11 +436,11 @@ const slidesInit = async () => { if (slides.length > 0) { showSlide(currentSlideIndex); container.style.display = "block"; - setTimeout(() => { - setInterval(() => { - updateCurrentSlide(currentSlideIndex + 1); - }, shuffleInterval); - }, 8000); + slideInterval = + new SlideTimer(function() { + updateCurrentSlide(currentSlideIndex + 1); + }, shuffleInterval); + } slides.forEach((slide) => { slide.addEventListener(