开发者

Pause a running setInterval function?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜