user1843261
user1843261

Reputation: 1

stop/pause this and restart it onclick

$(function(){
    $('.fadein img:gt()').hide();
    setInterval(function(){
      $('.fadein :first-child').fadeOut()
         .next('img').fadeIn()
         .end().appendTo('.fadein');}, 
      6800);
});

I'm hoping to stop this onclick of a button and start it on click of same button. any ideas?

Upvotes: 0

Views: 697

Answers (1)

karaxuna
karaxuna

Reputation: 26930

Try this:

$(function(){
    $('.fadein img:gt()').hide();
    var active = true;
    setInterval(function(){
      if(active) {
        $('.fadein :first-child').fadeOut()
           .next('img').fadeIn()
           .end().appendTo('.fadein');
      }
    }, 6800);

    $('#buttonId').click(function(){ active = !active; });
});

Thanks to Rikonator, here is better answer:

$(function(){
    $('.fadein img:gt()').hide();
    var active = true;
    var interval = setInterval(intervalFunction, 6800);

    $('#buttonId').click(function(){ 
       if(!active) 
          interval = setInterval(intervalFunction, 6800)
       else
          clearInterval(interval);
    });

    function intervalFunction(){
        $('.fadein :first-child').fadeOut()
           .next('img').fadeIn()
           .end().appendTo('.fadein');
    }
});

Upvotes: 1

Related Questions