首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当在浏览器选项卡之间移动时,setInterval跳转

当在浏览器选项卡之间移动时,setInterval跳转
EN

Stack Overflow用户
提问于 2012-02-26 03:26:13
回答 1查看 457关注 0票数 0

可能重复:

How can I make setInterval also work when a tab is inactive in Chrome?

http://www.datingjapan.co

我有setInterval更改图像如下:

代码语言:javascript
复制
    var initialFadeIn = 1000;           //initial fade-in time (in milliseconds)
    var itemInterval = 6000;            //interval between items (in milliseconds)
    var fadeTime = 2500;                    //cross-fade time (in milliseconds)

    var infiniteLoop = setInterval(function(){
        position1.eq(currentItem1).fadeOut(fadeTime);
        if(currentItem1 == numberOfItems1 -1) {currentItem1 = 0;}else{currentItem1++;}
        position1.eq(currentItem1).fadeIn(fadeTime);
    }, itemInterval);

我注意到,当我在Chrome浏览器选项卡之间移动时,当我回到站点时(间隔过后),图像会迅速跳转到下一个页面。能让这件事变得更顺利吗?还是让这发生在背景中?

任何有关这方面的信息都会很好。

thx

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-26 03:59:51

这种情况发生在setInterval上,但不发生在setTimeout上。诀窍是使用递归函数而不是setInterval。调用函数作为上一次动画的回调

代码语言:javascript
复制
function infiniteLoop() {
    setTimeout(function() {
        position1.eq(currentItem1).fadeOut(fadeTime);
        if (currentItem1 == numberOfItems1 - 1) {
            currentItem1 = 0;
        } else {
            currentItem1++;
        }
        position1.eq(currentItem1).fadeIn(fadeTime, infiniteLoop);
    }, itemInterval);
}

infiniteLoop();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9450268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档