2014-05-17 81 views
1

我有一個幻燈片放映功能,只是將背景更改爲七個不同的圖像。幻燈片功能

我有一個變量limit並開始索引。

它工作正常,但問題是我需要一直調用此函數(經常?),並且當索引等於極限時,該函數停止工作。 如何重置索引?

功能:

(function indexSlideShow(index) 
{ 
    var $limit = 7; 
    setTimeout(function() 
    { 
     $('#eventDetails').fadeTo(800, 1, function() 
     { 
      $('#eventDetails').css(
      { 
       "background" : "url(images/indexImgs/bigBg/" + index + ".jpg) no-repeat", 
       "background-position" : "cover" 
      }) 
     }).fadeTo(800, 1); 
    }, 4000); 

    index++; 

    if (index <= $limit-1) 
    { 
     setTimeout(function() 
     { 
      indexSlideShow(index) 
     },4000) 
    }; 
})(1); 
+0

'indexSlideShow(1)的''而不是indexSlideShow(指數)'? – Vincent

+0

它沒有幫助。函數停止索引7 – Victorino

回答

1

可以說,如果達到上限,該指數應該是你if (index <= $limit-1) {}語句之前復位。像這樣:

if (index == $limit-1) { 
    index = 0; 
} 

setTimeout(function() 
    { 
     indexSlideShow(index) 
    },4000) 
}; 
+0

它不起作用。當索引爲7時,函數不會再次啓動 – Victorino

+1

對不起。索引的重置應該發生在你的if(index <= $ limit-1)'之前。我更新了我的答案。 – creimers

+0

感謝您的方向它不是在你的方式工作,但我找到了解決方案 – Victorino

0

這裏是解決方案。可能是一些之一,如果你想重置* *的指標,你爲什麼不乾脆用它是有用的

/* index slide show function */ 
    (function indexSlideShow(index) { 

     var $limit = 7; 

      setTimeout(function() { 

      $('#eventDetails').fadeTo(800, 1, function(){ 

      $('#eventDetails').css({ 

       "background" : "url(images/indexImgs/bigBg/" + index + ".jpg) no-repeat", 
       "background-position" : "cover" 
      }) 

      }).fadeTo(800, 1); 

     }, 4000); 

     index++;   

     if (index == $limit) { 

      setTimeout(function(){ 

       indexSlideShow(0); 

      },4000); 

     } else if (index < $limit) { 

      setTimeout(function(){ 

       indexSlideShow(index); 

      },4000); 
     } 

    })(1);//end index slide show function