jQuery Timeout Function

January 22nd, 2009

jQuery does offer callbacks for most of its functions, but what if you want to delay/timeout a certain effect or function. Well with this article, this should help you achieve that.

I have been trying to search for a nice and reusable way to use jQuery to timeout effects or functions. We are familiar with using

setTimeout (function,timeout);

After putting in some data we can come up with something like this:

setTimeout (showBox(),2500);

Remember that the value 2500 is in MS (milliseconds), we equates to 2.5 seconds. When this function is called, it will execute 2500ms later. I was using this for one of my projects where I wanted two effects/functions not after, but during. I could have used very handy jQuery callback and load in a function, but it would have been executed after the first function is completed.

The solution for this? Quite simple, check out the jQuery $ function, which allows us to call jQuery as “$” the example is below.

(function($) {
    // Within this function $ is a reference to jQuery
    // I love this framework
})(jQuery);

Lets jump right into the belly of the beast and view the code to call our handy jQuery timeout function.

(function($){
	jQuery.fn.idle = function(time){
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	};
})(jQuery);

This might look like a bit overload, but it is really simple, we just learned how to create the skeleton of the function, so without it we:

jQuery.fn.idle = function(time){
	var i = $(this);
	i.queue(function(){
		setTimeout(function(){
			i.dequeue();
		}, time);
	});
};

You see on the first line above we create the idle function: jQuery.fn.idle. We created the variable I to store $(this), which is going to be element that is running the $.idle(); function.

We then use the jQuery queue(); function whose documentation can be found on the jQuery website. Then we attach the $(this); variable we created called: i to it, and create the setTimeout(), which then uses dequeue(); with our variable connected to it.

The last part, where we create the time parameter for the function, so we call the following code and:

$.idle(2500);

time gets replaced with the user input of 2500ms to the function. Say if you want to call a function you can use it in this context instead of the messy old fashion setTimeout(); function.

$('.myelement').fadeIn().idle(2000).fadeOut('slow');

The above simple reads: fade in my element, then wait 200ms or 2 seconds then slowly fade out the same element. BOOM! There it is, the quick n’ dirty method to timing functions out using jQuery.

jQuery Search And Highlight

December 20th, 2008

I was sitting in class and wanted to come up with some simple way to add a inline same page ajax search on a website. I came across the jQuery selector :contains(text), check out the documentation for this. Now the to the final product, lets check out the demo. We are going to have our main components for this:

  • The form (1 input and submit)
  • The :contains selector
  • The fadeIn() effect
  • The addClass()

The approach for the code is going to be like

  • User will enter term in form
  • Save data to a variable
  • Place variable in to :contains selector
  • Fade in the highlight class (using fadeIn() and addClass())

Read the rest of this entry »

Combining Selectables And Draggables Using jQuery UI

November 23rd, 2008

There are two powerful and fun elements that the jQuery UI provides. When both of them work together you will be able to come up with some creative web application uses for them. The two I am going to be covering are:

I am sure you all want to see what the final product will look like, check it out.

Read the rest of this entry »

Two Panel Slick Drop Down Using jQuery

November 16th, 2008
UPDATE: I have updated the code and the post here, I have optimized and narrowed down the number of lines, the new demo is up and running.

I use to use something similar to this on my older site to bring down content and a contact form on one side using jQuery, then about a year later I was thinking why not bring it back up, change some styles, and write a post for others to use. The concept is pretty simple.

When you press the selector, it will slide down panel 1, then using window.setTimeout, we will call the panel 2 to come down 100ms later, just enough of a delay where we get a nice effect. Once the panels slide down, the content within are faded in using fadeIn(); To close the panels we call a closePanels() that does the opposite effect when it is sliding down.

Read the rest of this entry »

Using the jQuery UI Slider

November 4th, 2008

Since I started working with jQuery and the UI. I have always wanted to work with the UI Slider. I always tried playing with it but I never got it execute an operation on slide. I created a slider to change the opacity of my background on my older temp site here at ryancoughlin.com.

UPDATE: Edit changes to the code with available demo below.javascript:;

UPDATE (9/16/09): Created a new jQuery UI Slider example, check it out.

Read the rest of this entry »