Search code examples
javascriptimpress.js

How to make impress.js slideshow auto play and loop


I have a slideshow I'd like to auto play and loop continuously. I've found snippets that let me set a global slide duration and use impress.next() with a setInterval() call to move forward, but then I lose the ability to have different durations for each slide.


Solution

  • I'm happy to share my solution. If you see room for improvement don't be shy. Hopefully this helps someone out there.

    <script>
      var impress = impress();
      impress.init();
    
      document.addEventListener('impress:stepenter', function(e){
        if (typeof timing !== 'undefined') clearInterval(timing);
        var duration = (e.target.getAttribute('data-transition-duration') ? e.target.getAttribute('data-transition-duration') : 2000); // use the set duration or fallback to 2000ms
        timing = setInterval(impress.next, duration);
      });
    </script>