Hi all,
I'm trying my first bit of jQuery code but am familiar with Java programming. I'm trying to create a function that loads a div with an image in it, which will fade in and then after 4 seconds move onto the next image. I want it to create an infinite loop but it doesn't work.

<script type="text/javascript">
		$(document).ready(function ()
	{
		var slide=true;
		while (slide==true)
		{
			var i=1;
			for(i=1;i<3;i++)
			{
				$(#main[i]).fadeIn('200');
				setTimeout(4000);
				$(#main[i].hide();
				if (i==2)
				{
					i=1;	
				}	
			}
		}
	});
	</script>

How do I fix this?

Cheers,

Recommended Answers

All 4 Replies

You WANT it to create an infinite loop? And what is the purpose of the outer loop? What is setTimeout _supposed_ to be doing (you're missing a function parameter to setTimeout)?

I want to create a image slideshow by placing an image in each div. I want it to continue to animate while the user is on the page. So each div will be displayed for 4 seconds, and then another div will fade in and display for 4 seconds.

You don't need a while loop to make a loop, just use setInterval(<repeat every N milliseconds>, <function to execute>);

Member Avatar for rajarajan2017

Loop never solve your issue, because in faction of seconds your loop got ended or become infinite. So please search for some timer function which set intervals like the above reply says. I suggested as a bad programming. Sure in JQuery timer functions would be there? Jquery handled many transition effects. So this is not the way you do. Get into that or google it for an example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.