I wrote a simple script for moving the pictures one after another at a certain interval, but nothing happens when you click on the slideshow stop, the pictures move as before. Here is the code:

lightbox.on('click.slide', '#slideHandler', function(e){ if ($(this).hasClass('startSlide')) { $(this).attr('class', 'stopSlide'); var slideshow = setTimeout(function sshow(){ imgright($('.imgtarget'), $('.imgstart'), $('#numberPhoto')); setTimeout(sshow, 4000); }, 4000); } else if ($(this).hasClass('stopSlide')) { clearTimeout(slideshow); $(this).attr('class', 'startSlide'); } return false; }); 
  • one
    make a fiddle, at first glance you have an error in that you are doing a second setTimeout without assignment to slideshow, just write function sshow () {imgright ($ ('. imgtarget'), $ ('. imgstart'), $ (' #numberPhoto ')); slideshow = setTimeout (sshow, 4000); }, 4000); and var slideshow should be announced above lightbox.on - zb '
  • This option is completely satisfied: the buttons change, the slideshow starts and stops. - CMargl

1 answer 1

In addition to the comment, I would generally simplify the code

 //изначально, нет startedSlide var slideshow; lightbox.on('click.slide', '#slideHandler', function (e) { var $this = $(this); e.stopPropagation(); e.preventDefault(); $this.toggleClass('startedSlide'); clearTimeout(slideshow); slideshow=setTimeout(sshow, 4000); function sshow() { if (!$this.hasClass('startedSlide')) return; imgright($('.imgtarget'), $('.imgstart'), $('#numberPhoto')); slideshow=setTimeout(sshow, 4000); } //return false; //не обязательно, просто чтобы показать точку выхода }); 
  • For some reason, this option works with the second click on the slideshow start button after creating the lightbox. And then the button works as it should. - CMargl
  • And yet my version with two classes is convenient (and most importantly works) for visually changing the Play and Pause buttons. - CMargl
  • Oh no, everything is fine, it works at the first click. I just forgot to delete the initially specified class in the script. - CMargl