【问题标题】:if counter reaches max count counter++ else counter = 0 in javascript如果计数器达到最大计数计数器 ++ 否则计数器 = 0 在 javascript
【发布时间】:2013-02-08 03:59:17
【问题描述】:

好的,所以我想如果我的计数器达到最大计数,它会重新开始,默认计数器编号为 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
         }
     }

好吧,我希望你们能帮我解决这个问题..

【问题讨论】:

  • 展示计数器的代码很多(格式错误)。
  • 你的标题回答了它。
  • setTimeout('nextPic()',10 * 1000)//setTimeout is here 有史以来最好的评论
  • 你有这么多没用的cmets!

标签: javascript arrays image max


【解决方案1】:

那是很多乱七八糟的代码。 我对实现的修复可能看起来像这样:

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
}

【讨论】:

  • 如果它证明可以回答您的问题/解决您的问题,请随时接受。
【解决方案2】:

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

if (picCount.length < maxCount.length) {

maxCountpicCount 只是数字。它们没有长度属性。改成这样:

if (picCount < maxCount) {

【讨论】:

    【解决方案3】:
    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 的长度。
    • 虽然通常不需要,但请尝试以分号结束所有语句。
    • 不要介意某些人的精英态度,但与此同时,在提出问题之前,请尽可能多地进行研究。

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多