Solved

create an infinite loop in jquery

Posted on 2011-09-26
10
495 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now