2012-02-26 68 views
0

可能重複:
How can I make setInterval also work when a tab is inactive in Chrome?的setInterval跳躍時的瀏覽器選項卡之間移動

http://www.datingjapan.co

我的setInterval改變圖片如下:

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

+0

這是在回答:http://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab -is-inactive-in-chrome我不確定如何鏈接到特定的帖子,但搜索鏈接:http://jsfiddle.net/7f6DX/31/你也可以去那個鏈接對於一個可行的方法。 – conrad10781 2012-02-26 03:31:12

回答

0

它發生在使用setInterval但與setTimeout的。竅門是使用遞歸函數而不是setInterval。調用函數作爲最後動畫的回調

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(); 
相關問題