2013-02-22 43 views
-3

好,所以我想,如果我的計數器達到最大計數,它開始了,用在這裏是默認值0計數器號碼是我的代碼:如果計數器達到最大計數計數器++否則計數器= 0在javascript

var picCount = 0; // global 
var maxCount = 4; 
//Pictures, to add more then 4 pics, add var picFive = "link to image here", var picSix ="bla", you get it.. add in picArray ,picFive and ,picSix 
//To change the time delay, change it at the body onload and on the setTimeout 
var picOne = "http://screenshots.nl.sftcdn.net/nl/scrn/3342000/3342167/modloader-for-minecraft-02-700x406.jpg" 
var picTwo = "http://media.moddb.com/images/downloads/1/31/30912/minecraft_blox.png" 
var picThree = "http://www.mupload.nl/img/rl6zeofbb.png" 
var picFour = "http://www.mupload.nl/img/rl6zeofbb.png" 

var picArray = [picOne, picTwo, picThree, picFour] 

// 
// gets next picture in array 
    function nextPic() { // check if adding 1 exceeds number of pics in array 
     if (picCount.length < maxCount.length) { 
      picCount = (picCount + 1 < picArray.length) ? picCount + 1 : 5000; 
      // build the image to write to page using the new pic reference 
      var build = '<img border="0" src="' + picArray[picCount] + '" width="649">\n'; 
      document.getElementById("imgHolder").innerHTML = build; 
      // repeat this every 10 seconds. 
      setTimeout('nextPic()', 10 * 1000) //setTimeout is here 
     } else { 
      picCount = (picCount - maxCount < picArray.length) ? picCount + 1 : 5000; 
      // build the image to write to page using the new pic reference 
      var build = '<img border="0" src="' + picArray[picCount] + '" width="649">\n'; 
      document.getElementById("imgHolder").innerHTML = build; 
      // repeat this every 10 seconds. 
      setTimeout('nextPic()', 10 * 1000) //setTimeout is here 
     } 
    } 

好吧,所以我希望你們可以幫助我這個..

+6

這是很多(格式錯誤)代碼來演示計數器。 – PeeHaa 2013-02-22 13:55:35

+0

您的標題可以解答它。 – epascarello 2013-02-22 13:59:39

+2

'setTimeout('nextPic()',10 * 1000)// setTimeout在這裏有史以來最好的評論 – jbabey 2013-02-22 14:07:07

回答

0

這是很多雜亂的代碼。 我對實現修復可能會是這個樣子:

var currentPic = 0; 
var picOne = "http://screenshots.nl.sftcdn.net/nl/scrn/3342000/3342167/modloader-for-minecraft-02-700x406.jpg" 
var picTwo = "http://media.moddb.com/images/downloads/1/31/30912/minecraft_blox.png" 
var picThree = "http://www.mupload.nl/img/rl6zeofbb.png" 
var picFour = "http://www.mupload.nl/img/rl6zeofbb.png" 

var picArray= [picOne,picTwo,picThree,picFour] 

function nextPic() { 
    if (currentPic < picArray.length) {currentPic++;} 
    else {currentPic = 0;} 

    var build='<img border="0" src="'+picArray[currentPic]+'" width="649">'; 
    document.getElementById("imgHolder").innerHTML=build; 
    // repeat this every 10 seconds. 
    setTimeout('nextPic()',10 * 1000)//setTimeout is here 
} 
+0

如果它證明回答您的問題/解決您的問題,請隨時接受答案。 – mariusnn 2013-02-24 02:26:44

0

儘管我確信存在於你的代碼中的許多其他問題,我相信這條線是您的特定問題的原因在問題解決:

if (picCount.length < maxCount.length) { 

maxCountpicCount只是數字。他們沒有長度屬性。它改成這樣:

if (picCount < maxCount) { 
0
var currentPic = 0; 
var picArray= ["http://screenshots.nl.sftcdn.net/nl/scrn/3342000/3342167/modloader-for-minecraft-02-700x406.jpg", 
       "http://media.moddb.com/images/downloads/1/31/30912/minecraft_blox.png", 
       "http://www.mupload.nl/img/rl6zeofbb.png", 
       "http://www.mupload.nl/img/rl6zeofbb.png"]; 

function nextPic() { 
    (currentPic < picArray.length) ? currentPic++ : currentPic = 0; 
    var build='<img border="0" src="'+picArray[currentPic]+'" width="649">'; 
    document.getElementById("imgHolder").innerHTML=build; 
} 

setTimeout('nextPic()',10 * 1000); 

我做了一些改動,使你的代碼更加清晰。 一些提示:

  • 無需將圖像URL存儲在變量中,然後將它們放入數組中。只需用它們初始化你的數組。
  • 不要重複自己。每當你發現你在多個地方使用完全相同的代碼時,你可能需要重新思考你是如何處理這個問題的。
  • 查找「三元操作符」。在我看來,它使得簡單的條件語句更易於閱讀。
  • 不需要使用maxCount - 最大計數將是你的picArray的長度。
  • 雖然通常不需要,但嘗試用分號結束所有語句。
  • 不要介意一些人的精英態度,但同時在提問前儘可能多地去研究。