Home » jQuery

jQuery: Set time interval between events with queue function

26 January 2010 11,358 views Popularity: 31% Share/Bookmark

email

You can use jQuery queue() function with setTimeout() function to set some time interval between events in jQuery. Like, you have run one event and wanted the browser to wait for some time to run the next event. At this instance, queue function is very helpful.

Suppose, you wanted an image to fade in and out. You can use fadeIn() and fadeOut() jQuery functions to do so. But when you like to wait for some time between fade in and fade out then you can use the queue() function. Remember that, .dequeue() is necessary to write in a queue() so that the next function in line executes.

View Demo || Download Code

Here is the full source code.

<html>
<head>
<title>jQuery: Queue</title>
<script src="jquery-1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
		var img = $(".img");
		for(var i=0;i<4;i++) {
			img.queue(function(){
				setTimeout(function(){
					img.dequeue();
				}, 1000);
			});

			img.fadeOut("slow");

			img.queue(function(){
				setTimeout(function(){
					img.dequeue();
				}, 1000);
			});

			img.fadeIn("slow");
		}
	});
</script>
</head>
<body>
<div align="center">
	The image will fade in and out for 5 times.<br/>
	<img class="img" src="hills.jpg" alt="image" />
</div>
</body>
</html>

View Demo || Download Code

Cheers,

Related posts:

  1. jQuery: A simple Slideshow
  2. jQuery: Animate and Transfer effect with Image
  3. jQuery: Preview Image with Zoom Effect
  4. jQuery: Grey out background and preview image as popup
  5. jQuery: Preview Image with Tooltip Effect
  6. jQuery: Print array and object
  7. jQuery: How to replace string, div content and image src?
  8. Using jQuery & AJAX: Populate Selection List
  9. Magento jQuery: How to use them together?
  10. Multiple file upload with jQuery and php
  • http://www.elitemcp.com/all-about-kaldewei-baths-europes-number-one-tub-manufacturer/ All About Kaldewei Baths Europe's Number One Tub Manufacturer | Elite Bathrooms

    [...] jQuery: Set time interval between events with queue function … [...]

  • Gunnar

    Hello!
    Seeing the issues you’re working on, you could probably help me: I’m looking for a kind of time event for jQuery, in order to make an ajax request to the database in regular time intervals. For instance, if you want to update a shoutbox every 5 seconds.
    Actually, when I see your „recent tweets“ box on your page, you seem to use some function like that (at least, this is similar to what I have in mind). Can you give me a hint how I can tell my script (using jQuery) to execute an ajax request in regular time intervals?

    Thank you in advance!

    Gunnar