Solved

create an infinite loop in jquery

Posted on 2011-09-26
10
500 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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:gurvinder372
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

792 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