Solved

create an infinite loop in jquery

Posted on 2011-09-26
10
499 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery keyup 4 20
Pass asp session variable to a js function in an asp script 6 20
Call a function within the ASP code 4 12
tiny glitch in my main slider 3 24
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-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

831 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