2016-08-11 58 views
-1

这是我的代码,我不确定我是否使用正确的循环,但基本上这是要做的。我试图让数组中的每个src打印出每一秒,但当试图打印出myPix数组中的第二项时,myPix [thisPic]变得未定义。我真的很乐意得到一些帮助。谢谢!了解Java脚本中的循环

function nextPic() { 
    count = -1; 
    thisPic = 0; 

    if (thisPic == count) { 
     document.write("This shouldn't ever happen"); 
     return false; 
    } else { 
     count++; 
     var myPix = new Array("images/slideshow/Jellyfish.jpg", "images/slideshow/Penguins.jpg", "images/slideshow/Lighthouse.jpg"); 
     slideTag = document.getElementsByTagName("img")[1]; 
     slideTag.src = myPix[thisPic]; 
     currPic = slideTag.src; 
     document.write(myPix[thisPic] + "<br />"); 
     document.write(currPic); 
    } 

    return false; 
} 
setInterval(nextPic, 1000); 
+0

它确实有助于格式化您的代码,因此可以轻松读取它。 – Dropout

+1

你似乎并没有在任何地方更新'thisPic',并且你没有在任何地方使用'count',只是将它与'thisPic'比较,它总是'false'。因此,'myPix [thisPic]'不应该导致'undefined'。 'myPix'是一个至少包含一个元素的数组。你有没有从你的代码中省略一些东西? –

+0

在页面加载完成后使用'document.write' [某些时候会严重结束](http://stackoverflow.com/questions/5985740/not-allowed-document-write-after-page-load) 。 –

回答

0

代码不起作用,因为“循环”没有共享变量。每次迭代都应该在同一个数组和相同的迭代器上工作,在你的代码中,每次迭代都会创建新的数组和新的迭代器,所以每次迭代都会做同样的事情。

为了正确地工作,每一个循环迭代必须:

  1. 取当前图像SRC从阵列(声明外循环一个变量)
  2. 集它img元素
  3. 更改当前变量电流+ 1(电流可变外循环delared)

如果我们的电流更该数组大小然后 - 端(clearInterval)或设定电流0 - 然后一次又一次地开始......

//array declaration should be outside of loop 
 

 
var myPix = new Array("https://www.gravatar.com/avatar/50fbdd1dcbe4763a33a0e212f6a632c8?s=32&d=identicon&r=PG&f=1", "https://www.gravatar.com/avatar/5be6299fb284071a9fd2cc2cc0b9090a?s=32&d=identicon&r=PG", "https://i.stack.imgur.com/mlB9c.jpg?s=48&g=1"); 
 

 
var currentPic=0; //this is number of current picture 
 

 
function nextPic(){ 
 
      
 
     if (currentPic>= myPix.length){ 
 
      
 
      // here example how to stop: 
 
      //clearInterval(interval); 
 
      //return; 
 
      
 
      //we loop it over again from begging 
 
      currentPic=0; 
 

 
     } 
 
    
 
     //get img in page - image by currentPic 
 
     slideTag = document.getElementsByTagName("img")[1];//second 
 
     //set src on this slideTag from array 
 
     slideTag.src = myPix[currentPic]; 
 
    
 
     currentPic++; 
 

 
     
 
} 
 

 
var interval=setInterval (nextPic, 1000);//save interval in variable to use it to stop
<h1>images</h1> 
 
<img><br/> 
 
<img><br/> 
 
<img><br/>

我采取例如化身从计算器,显示工作例如:)

+0

非常感谢。我使代码比我需要的更复杂。我真的很棒 –

0

按理解你的代码,我有figurout您的问题

var myPix = ["images/slideshow/Jellyfish.jpg", "images/slideshow/Penguins.jpg", "images/slideshow/Lighthouse.jpg"]; 
    counter=0; 
interval = setInterval (nextPic, 1000); 
    function nextPic(){ 
     if(counter >= myPix.length){ 
      document.write("This shouldn't ever happen"); 
      clearInterval(interval); 
     } 
     else{ 

      slideTag = document.getElementsByTagName("img")[1]; 
      slideTag.src = myPix[counter]; 
      currPic = slideTag.src; 
      document.write(myPix[thisPic] + "<br />"); 
      document.write(currPic); 
      counter++; 
     } 

    }