-3

好的,所以我想如果我的计数器达到最大计数,它会重新开始,默认计数器编号为 0 这是我的代码:

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
         }
     }

好的,所以我希望你们能帮助我解决这个问题..

4

3 回答 3

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 放入数组之前,无需将它们存储在 vars 中。只需用它们初始化您的数组。
  • 不要重复自己。每当您发现自己在多个地方使用完全相同的代码时,您可能需要重新考虑如何解决问题。
  • 查找“三元运算符”。在我看来,它使简单的条件语句更易于阅读。
  • 无需使用 maxCount - 最大计数将是您的 picArray 的长度。
  • 尽管通常不需要,但请尝试以分号结束所有语句。
  • 不要介意某些人的精英态度,但与此同时,在提出问题之前尽可能多地进行研究。
于 2013-02-22T15:20:41.317 回答
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
}
于 2013-02-22T14:07:58.667 回答
0

尽管我确信您的代码中存在许多其他问题,但我相信这一行是您在问题中解决的特定问题的原因:

if (picCount.length < maxCount.length) {

maxCount并且picCount只是数字。它们没有长度属性。将其更改为:

if (picCount < maxCount) {
于 2013-02-22T14:10:04.543 回答