Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create an infinite loop in jquery

Posted on 2011-09-26
10
Medium Priority
?
520 Views
Last Modified: 2012-06-27
I am learning javascript and jquery and I am trying to create what I thought would be very simple loop.  I am fading background images in and out and looping through six images but when I run my code the browser becomes non responsive.


<script type="text/javascript">
  var aImages = new Array();
  var imageNum = 0;
  aImages[0] = "img/limerick.jpg";
  aImages[1] = "img/launch.jpg";
  aImages[2] = "img/graduates.jpg";
  aImages[3] = "img/portrait.jpg";
  aImages[4] = "img/wedding.jpg";
  aImages[5] = "img/engagement.jpg";
  aImages[6] = "img/wedding2.jpg";
  $(document).ready(function() {
    while (imageNum < 10) {
      if (imageNum > 6) {
        imageNum = 0;
		}
      $("img#bg").attr("src", aImages[imageNum]);
      $("img#bg").load
      $("img#bg").fadeIn(1000);
      setTimeout(function() {$("img#bg").fadeOut(1000);},3000);
      imageNum ++;
      }
	});
</script>

Open in new window

0
Comment
Question by:iepaul
10 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36597487
setTimeout is a function that will never stop unless you call clearTimeout

http://www.w3schools.com/js/js_timing.asp
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36597489
<script type="text/javascript">
  var aImages = new Array();
  var imageNum = 1;
  aImages[0] = "img/limerick.jpg";
  aImages[1] = "img/launch.jpg";
  aImages[2] = "img/graduates.jpg";
  aImages[3] = "img/portrait.jpg";
  aImages[4] = "img/wedding.jpg";
  aImages[5] = "img/engagement.jpg";
  aImages[6] = "img/wedding2.jpg";
  $(document).ready(function() {
    while (imageNum < 7) {
     
      $("img#bg").attr("src", aImages[imageNum]);
      $("img#bg").load
      $("img#bg").fadeIn(1000);
      setTimeout(function() {$("img#bg").fadeOut(1000);},3000);
      imageNum ++;
      }
      });
</script>
0
 
LVL 15

Expert Comment

by:Eyal
ID: 36597502
<script type="text/javascript">
  var aImages = new Array();
  var imageNum = 1;
  aImages[0] = "img/limerick.jpg";
  aImages[1] = "img/launch.jpg";
  aImages[2] = "img/graduates.jpg";
  aImages[3] = "img/portrait.jpg";
  aImages[4] = "img/wedding.jpg";
  aImages[5] = "img/engagement.jpg";
  aImages[6] = "img/wedding2.jpg";
  $(document).ready(function() {
       setTimeout(changeimg,3000)

      });
function changeimg(){
      var img = $("img#bg");
      img.fadeOut(1000);
      if(++imageNum==7)imageNum=0;
      img.attr("src", aImages[imageNum]).load().fadeIn(1000);
}
</script>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:iepaul
ID: 36597614
eyal thanks your for your code but it does not appear to loop.  it displays the first image then stops.
0
 
LVL 13

Expert Comment

by:dsmile
ID: 36597630
Problem lies in line 12 to 15.
Exit condition of the loop is imageNum = 10, but when it is 7, you reset it --> the loop will never end.
Use either suggestion of Pratima or Eyal is fine :)
0
 

Author Comment

by:iepaul
ID: 36597634
i want to keep the infinite loop.  the exit condition is never expected to be met.  the problem is the browser becomes no responsive.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36597655
callMeAgainLater();
function callMeAgainLater()
{
   ...your code here...
   setTimeout( "callMeAgainLater()", 1000 );
}

above code will keep on looping
0
 
LVL 15

Expert Comment

by:Eyal
ID: 36597873

<script type="text/javascript">
  var aImages = new Array();
  var imageNum = 1;
  aImages[0] = "img/limerick.jpg";
  aImages[1] = "img/launch.jpg";
  aImages[2] = "img/graduates.jpg";
  aImages[3] = "img/portrait.jpg";
  aImages[4] = "img/wedding.jpg";
  aImages[5] = "img/engagement.jpg";
  aImages[6] = "img/wedding2.jpg";
  $(document).ready(function() {
       setTimeout(changeimg,3000)

      });
function changeimg(){
      var img = $("img#bg");
      img.fadeOut(1000);
      if(++imageNum==7)imageNum=0;
      img.attr("src", aImages[imageNum]).load().fadeIn(1000);
      setTimeout(changeimg,3000)
}
</script>

Open in new window

0
 

Author Comment

by:iepaul
ID: 36890109
Eyal that code works but strange things are happening with the pictures.  A picture appears, fades out, then fades in again.  The next picture appears, fades out and fades in again.

It does loop correctly though.
0
 
LVL 15

Accepted Solution

by:
Eyal earned 2000 total points
ID: 36893756
function changeimg(){
      var img = $("img#bg");
      img.fadeOut(1000', function() {
    // Animation complete.
   if(++imageNum==7)imageNum=0;
      img.attr("src", aImages[imageNum]).load().fadeIn(1000);
      setTimeout(changeimg,3000)
});
     }
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question