Jump to content

Is it possible to run an infinite loop with jQuery each() function


drayarms

Recommended Posts

Hello folks, well the topic is self descriptive.  I used the each() function to run a loop on a group of divs.  The function takes a callback setTimeOut function to delay transitions, and this function in turn takes as its callback, two animate and a delay function which is basically the juice of the program.  The end result is, each div slides in from the left , spends a few seconds on the screen, then slides out, followed by the next div.  I want this to continue infinitely, not end with the last div.  Is it possible to accomplish that with the each function?  If so, what callback function do I use to accomplish that and where?  Here is my code.

 

html

		<div class ="slide" id ="slide1">





		</div> <!--closes slide1-->









		<div class ="slide" id ="slide2">





		</div> <!--closes slide2-->











		<div class ="slide" id ="slide3">





		</div> <!--closes slide3-->











		<div class ="slide" id ="slide4">





		</div> <!--closes slide4-->


 

css

.slide{width:400px;height:240px;display:none;position:relative;top:40px;left:20px}


#slide1{background:red}


#slide2{background:green}


#slide3{background:blue}


#slide4{background:yellow}



 

 

jQ

	<script type = "text/javascript">





	$(document).ready(function() {  


		$.each($('.slide'), function(i, val){

			setTimeout(function(){

				$(val).animate({width:"toggle"}, '4000').delay(3000).animate({width: "toggle"},'4000'); 


			},4500 + (i*4500)); 

		});  







	});    







	</script>

Link to comment
Share on other sites

I'm not too up to speed on JQuery, but I would think all you need to do is put the each() loop inside of another loop. Something like

while(1==1)
{
    $.each($('.slide'), function(i, val)
    {
        setTimeout(function()
        {
            $(val).animate({width:"toggle"}, '4000').delay(3000).animate({width: "toggle"},'4000'); 
        },4500 + (i*4500)); 
    }
}

So, on each loop of the outer loop, the each() loop is reset.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.