0

I'm trying to make an automatic slideshow in css and js. I've got a function that infinitely loops through an array of images (in the slideshow) after a specified time.

function autoSlide(){
    carouselSlide.style.transition = "transform 1s ease-in-out";
    counter++;
    carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';

    t = setTimeout("autoSlide()", time);
}

But I want to reset the timer time if I manually change the slide by pressing a button. I tried this but it didn't work:

function autoSlide(){
    carouselSlide.style.transition = "transform 1s ease-in-out";
    counter++;
    carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';

    nextBtn.addEventListener('click',()=>{
        clearTimeout(t);
    })

    t = setTimeout("autoSlide()", time);
}

Any help would be much appreciated.

Thanks.

1
  • Are you sure it's the time you want to reset rather than the counter? Is time incrementing over time? Commented Jun 20, 2021 at 21:42

4 Answers 4

1

Please use setInterval instead of setTimeout. Like this:

function autoSlide(){
    carouselSlide.style.transition = "transform 1s ease-in-out";
    counter++;
    carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
}
var t = setInterval(autoSlide, time)
nextBtn.addEventListener('click',()=>{
    clearInterval(t);
    t= setInterval(autoSlide, time)
})
Sign up to request clarification or add additional context in comments.

1 Comment

this would stop the timer for ever. the goal is to restart the timer.
1

I don't see that you're defining the interval variable outside of the function scope so it might be getting lost. Also, shouldn't this be a setInterval vs setTimeout? You could use either, but if your goal is to infinitely repeat until some condition, setInterval is more appropriate

let interval = setInterval(autoSlide, time); // variable being initialized outside function scope
nextBtn.addEventListener('click',()=>{
  clearInterval(interval);
  interval = setInterval(autoSlide, time)
})
function autoSlide(){
    carouselSlide.style.transition = "transform 1s ease-in-out";
    counter++;
    carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
    // ... presumably other logic to progress your slideshow every 'time'
}

Comments

1

One way to do it would be to recreate the timer when you click. Not only delete the timer, but also recreate it.

nextBtn.addEventListener('click',()=>{
    clearTimeout(t);
    t = setTimeout(autoSlide, time);
})

ps: it's always good to learn, but dont try too much to reinvent the wheel. use this for exemple : https://owlcarousel2.github.io/OwlCarousel2/demos/basic.html responseive, auto, touch support, etc...

Comments

0

If this is a carousel you want your timeout to be the same but to reset the counter when you click the button.

Here's a function that sets up your timer, and your button, and returns a closure (a function that retains the information in its preceding lexical environment after it's been returned) that controls the timeout.

function handler() {

  const time = 1000;
  let counter = 0;

  const button = document.addEventListener('click', () => counter = 0);

  return function loop() {

    // Your transform code goes here
    console.log(counter);
    counter++;
    setTimeout(loop, time);
  }

}

// autoSlide is the returned function
const autoSlide = handler();
autoSlide();
<button>Stop the sliding!</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.