PhotoSwipe autoplay with next()

1k Views Asked by At

I try to make the photoswipe JS Image Gallery run as a slideshow, with this function:

function gallery_autoplay() {

    gallery.next();
    setTimeout(gallery_autoplay(),3000);

}

setTimeout(gallery_autoplay(),3000);

but ei get an error at line 1088 of photoswipe.js

2

There are 2 best solutions below

0
On

You need setInterval not setTimeout

function gallery_autoplay() {
    gallery.next();
}
setInterval(gallery_autoplay,3000);
0
On

You are not including your whole code, but if I were to take a guess it would be that the variable you use the .next() function on doesn't exist or is initiated wrong.

Try this:

var gallery; //As a global variable
//then when you init the gallery just use that same variable
//for instance
function OpenPhotoSwipe() {
  var pswpElement = document.querySelectorAll('.pswp')[0];
  var items = [{
    src:'',
    w:1,
    h:1
  }]
  var options = { modal:false,bgOpacity:0.7 };
  gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
  gallery.init();
}
//And then finally use setInterval to queue the transition
setInterval(function() {
  gallery.next();
},3000);

Hope this helps