0

I have 5 slides, which are changing after 5 second or on mouse click(Done with SetInterval function, which triggers 'click')

Problem is following, for example: Lets say it's set to #slide_1, when 4 seconds passed and i manually click on #slide_2, next slide(#slide_3) comes active after 1 second. It's all SetInterval, that triggers click and i have no idea how to add 5 second interval again, between manual and SetInterval clicks..

Here is my code,

    setInterval(function() {
       if(!$('.buttons > ul ').children('.butt_press').next().length){
            $('.buttons > ul').children(':first').trigger('click');
       }
       else{
            $('.buttons > ul').children('.butt_press').next().trigger('click');
       }
   }, 5500);

thanks

  • I believe you want to clear your interval first then set it again on slide change. That way it exits the first and restarts again with full time. – below9k Sep 17 '14 at 13:54
  • Try this solution: http://stackoverflow.com/questions/1909441/jquery-keyup-delay – K K Sep 17 '14 at 13:57
  • shouldn't you be using children().each(function{}) instead of next(), if you want to iterate and do something (trigger click) on the child elements ? – vreddy Sep 17 '14 at 14:24

1 Answers1

1

setInterval returns an interval id which you can use to clear the interval and then re-create it.

var nextSlide = function () {
   if(!$('.buttons > ul ').children('.butt_press').next().length){
        $('.buttons > ul').children(':first').trigger('click');
   } else {
        $('.buttons > ul').children('.butt_press').next().trigger('click');
   }
};

var interval = setInterval(nextSlide, 5000);

$('some_selector').on('click', function () {
  nextSlide();
  clearInterval(interval);
  interval = setInterval(nextSlide, 5000);
});
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160