如果计数器在javascript中达到最大计数计数器++ else counter = 0

时间:2013-02-22 13:54:26

标签: javascript arrays image max

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

好的,所以我希望你们可以帮助我...

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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

if (picCount.length < maxCount.length) {

maxCountpicCount只是数字。他们没有长度属性。将其更改为:

if (picCount < maxCount) {

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