2016-01-25 47 views
1

我在JavaScript中使用此代碼,並且此圖像1-6仍在重複。但我希望當它在6時停下來,不再重複。我該怎麼做?Javascript - 無重複圖像

<script> 
    function displayNextImage2() { 
       y = (y === images2.length - 1) ? 0 : y + 1; 
       document.getElementById("img2").src = images2[y]; 
      } 

      function displayPreviousImage2() { 
       y = (y <= 0) ? images2.length - 1 : y - 1; 
       document.getElementById("img2").src = images2[y]; 
      } 

      function startTimer2() { 
       setInterval(displayNextImage2, 5000); 
      } 

      var images2 = [], y = 0; 
      images2[0] = "images/one.PNG"; 
      images2[1] = "images/two.PNG"; 
      images2[2] = "images/three.PNG"; 
      images2[3] = "images/four.PNG"; 
      images2[4] = "images/five.PNG"; 
      images2[5] = "images/six.PNG"; 
</script> 

感謝每一個答案。

回答

0

變化displayNextImage2方法

function displayNextImage2() 
{ 
    if (y <= images2.length - 1) 
    { 
    document.getElementById("img2").src = images2[y]; 
    y++; 
    clearInterval(timer2); 
    } 
} 

確保您清除間隔一樣,所以你需要保持它的參考

var timer2 = ""; 
function startTimer2() 
{ 
    timer2 = setInterval(displayNextImage2, 5000); 
} 

到達後,將不再重複最後一張圖片