0

The following works but I would like the 'playSlideshow()' to stop once I click on '$('.js-slideshow span')'?

$("img.clickable").click(function(e){// showing and hiding property images
    e.stopPropagation();
    $('.js-slideshow span').delay(500).fadeIn();
    var $window = $(window);
    var $windowWidth = $(window).width();
    var clickableImage = $(this);   
    var clickableImageRightPx = clickableImage.css('right');
    var clickableImageRightInt = parseInt(clickableImageRightPx , 10);

    if (clickableImageRightInt == -108) {
        playSlideshow();            
    }

});

$('.js-slideshow span').click(function(){
    $(this).fadeOut(function(){
        $('.showImg').removeClass("showImg");
            // stop playSlideshow();
    });
});

function playSlideshow(){

  $(".js-slideshow > img:gt(0)").delay(500).fadeOut();

  setInterval(function() { 
      $('.js-slideshow > img:first')
      .fadeOut(1000)
      .next()
      .fadeIn(1000)
      .end()
      .appendTo('.js-slideshow');},  3000);

       }

 }
3
  • 1
    There is no way to stop a function from outside the function. Please provide more information about what playSlideshow() is. Commented Feb 13, 2014 at 16:12
  • 1
    share playSlideshow Commented Feb 13, 2014 at 16:12
  • Agree with @smdrager. This is possible if playSlideshow is calling another function on some interval using setInterval, but we don't know unless we see playSlideshow. Commented Feb 13, 2014 at 16:14

1 Answer 1

1

Use the interval reference from the playSlideshow method and user clearInterval() to stop it in the js-slideshow span click handler

$('.js-slideshow span').click(function () {
    clearInterval(slideshowInteval)
    $(this).fadeOut(function () {
        $('.showImg').removeClass("showImg");
        // stop playSlideshow();
    });
});

var slideshowInteval;

function playSlideshow() {

    $(".js-slideshow > img:gt(0)").delay(500).fadeOut();

    slideshowInteval = setInterval(function () {
        $('.js-slideshow > img:first')
            .fadeOut(1000)
            .next()
            .fadeIn(1000)
            .end()
            .appendTo('.js-slideshow');
    }, 3000);

}
Sign up to request clarification or add additional context in comments.

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.