Matej Posted April 16, 2014 Share Posted April 16, 2014 Hello , <img id="matej" src="http://thegraphicsfairy.com/wp-content/uploads/blogger/-72LEqJJmnH0/TtoypcjmR8I/AAAAAAAAPjA/3AbGm3448JU/s1600/alarm%2Bclock%2Bsmall%2Bvintage%2Bimage%2Bgraphicsfairy2.jpg" width=100px; height="50px"> <script>var lol=new Array();lol[0]="https://guardian-registration.s3.amazonaws.com/networks/network/illustration/Illustration-small_business.png"lol[1]="http://peach.blender.org/wp-content/uploads/poster_rodents_small.jpg";lol[2]="http://upload.wikimedia.org/wikipedia/en/8/85/Small_soldiers_movie_poster.jpg";function torko(){ for(i=0;i<lol.length;i++){var x=document.getElementById("matej");x.src=lol[i];}setTimeout("torko()",3000); } window.onload=torko;</script> why isnt my for loop working? (it does not slide the image) ; also if i use if(i<lol.length-1){i++}else{i=0} it works , can someone explain why please? Link to comment Share on other sites More sharing options...
davej Posted April 16, 2014 Share Posted April 16, 2014 <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Matej</title><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>window.onload = function(){setTimeout(torko,3000);}var i=0;var lol = [];lol[0]="https://guardian-registration.s3.amazonaws.com/networks/network/illustration/Illustration-small_business.png";lol[1]="http://peach.blender.org/wp-content/uploads/poster_rodents_small.jpg";lol[2]="http://upload.wikimedia.org/wikipedia/en/8/85/Small_soldiers_movie_poster.jpg";function torko() {var x=document.getElementById("matej");if(i>=lol.length){i=0}x.src=lol[i++];setTimeout(torko,3000);} </script></head><body><img id="matej" src="http://thegraphicsfairy.com/wp-content/uploads/blogger/-72LEqJJmnH0/TtoypcjmR8I/AAAAAAAAPjA/3AbGm3448JU/s1600/alarm%2Bclock%2Bsmall%2Bvintage%2Bimage%2Bgraphicsfairy2.jpg" width=100px; height="50px"/> </body> </html> Link to comment Share on other sites More sharing options...
Matej Posted April 16, 2014 Author Share Posted April 16, 2014 (edited) I know that it works if i use if(), but why it does now work with for{} aswell? //thanks for answer Edited April 16, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted April 16, 2014 Share Posted April 16, 2014 A for-loop doesn't make any sense for this situation. This function must terminate so that setTimeout() can call it again in 3000 milliseconds. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2014 Share Posted April 16, 2014 Your code works just fine, it just doesn't do what you expected. for(i=0;i<lol.length;i++){ var x=document.getElementById("matej"); x.src=lol[i];}That loop is going to run 3 times, as fast as possible. The first time it's going to set the source to the first image, then it's going to set the src to the second image, then to the third image. It's going to do that as fast as possible, you probably won't notice the change. Then you schedule the function to run again after 3 seconds and it does the same thing, changes the source of the image 3 times as fast as possible. You don't want it to change as fast as possible, you want it to change once, then wait a while, then change again, wait a while, etc. That is not what a for loop does. 1 Link to comment Share on other sites More sharing options...
Matej Posted April 16, 2014 Author Share Posted April 16, 2014 Thank you very much for good explanation sir! Link to comment Share on other sites More sharing options...
Matej Posted April 19, 2014 Author Share Posted April 19, 2014 (edited) Just for info , while loop doesnt make what for in term of chaning speed right? for example function swapImage() { var i=0;document.slide.src = path[i]; while(i < path.length -1 ){ i++;} ;if(i>path.length) {i = 0;} ; setTimeout("swapImage()",3000); } window.onload=swapImage; dont work >{ Edited April 19, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted April 20, 2014 Share Posted April 20, 2014 function swapImage() { var i=0; // set i=0document.slide.src = path[i]; //assign path[0] to the src attribute of document.slide, if it exists.while(i < path.length -1 ){ i++;} ; //set i=1,2,3 up to (path.length - 2)if(i>path.length) {i = 0;} ; //useless check for i > path.length (which will never happen)setTimeout("swapImage()",3000); //schedule a call to "swapImage()" in 3 seconds.} It doesn't work because it is garbage. Please tell me in words what you think it is supposed to do. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 21, 2014 Share Posted April 21, 2014 Again, that code works just fine, it's doing exactly what you tell it to do. What you're telling it to do is to set document.slide.src to path[0], whatever path is, and then you run a loop for no apparent reason, then schedule the function again. It's doing all of that just like you told it to. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now