1

I have a slide show use setInterval 5sec, each slide should stop 5 sec.

When user click previous button, slide will go back previous image.

My problem is when user click previous, time still counting. (previous image only stop 3sec not 5sec)

Is any way to reset time when user click?

setInterval(function(){
  //start slide...
},5000);


$('#previous').click(function(){
    //go back to previous slide and reset time
});
Ben
  • 2,562
  • 8
  • 37
  • 62

2 Answers2

11

Like this:

var interval;
 var timer = function(){
 interval = setInterval(function(){
  //start slide...
},5000);
};


$('#previous').click(function(){
    //go back to previous slide and reset time
   clearInterval(interval);
   timer()
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

Put your interval-setting code in a function. Assign your interval to a globally-declared variable.

When resetting, you can clear the interval and start it again.

You could start the interval on DOM load by calling startInt(), or just by triggering a click on #previous, whichever way is more appropriate for your situation.

var myInterval = undefined,
    startInt = function(){
    myInterval = setInterval(function(){

    }, 5000);
};

$('#previous').click(function(){
    if(typeof myInterval != 'undefined'){ clearInterval(myInterval); }
    startInt();
}).click();
George
  • 36,413
  • 9
  • 66
  • 103