1

I have 4 functions running on setInterval (tscrolls) which just animate a div's top location every couple of seconds as soon as the document is loaded.

var intervalFunctions = [ tScroll1, tScroll2, tScroll3, tScroll4 ];
var intervalTimer = 3000;
window.setInterval(function(){
  intervalFunctions[intervalIndex++ % intervalFunctions.length]();
}, intervalTimer);

Is there a way to pause this on mouseenter or hover?

Dim Panda
  • 69
  • 2
  • 6
  • possible duplicate of [How do I pause a windows.setInterval in javascript?](http://stackoverflow.com/questions/7279567/how-do-i-pause-a-windows-setinterval-in-javascript) – Felix Kling Sep 10 '11 at 11:16

1 Answers1

7

The simplest would be to modify your code to reference a variable (here, hold) which is set on mouseover, and cleared on mouseout:

window.setInterval(function(){ 
    if(hold) {
        return;
    }
    intervalFunctions[intervalIndex++ % intervalFunctions.length](); 
}, intervalTimer); 

This approach will get you most of the way, and if you want to achieve specific behavior you can tweak it to taste.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • +1: I think this `hold` approach is nicer than using `clearInterval` and then restarting it with `setInterval`. – nnnnnn Sep 10 '11 at 12:28