Writings, notes, and work of a Boston based designer

jQuery Timeout Function Jan 22 + 2 comments

written by: ryan coughlin

UPDATE: Edited the code thanks to Adrian Green. Thanks for the comment

jQuery does offer callbacks for most of its functions, but what if you want to delay or timeout a certain effect or function there isn’t a function made for that. This article should help you achieve that.

Old School JavaScript

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 the traditional JavaScript function setTimeout():

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.

Let’s Start Delaying Effects – The Code

Lets jump right into the belly of the beast and view the code to call our handy jQuery timeout function. Essentially we have a small jQuery plugin. We define the function as: $.idle()

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 2000ms, slowly fade out the same element. BOOM! There it is, the quick n’ dirty method to delaying functions using jQuery.

Take a peak at some more?

  • http://www.bestqualitypools.info mike villa

    i am looking for someone to take over a web sight i have , and make some changes to it for me. the guy who made it up now works for a big company and is not abel to free lance any more . he had told me it is Ruby on Rails and jQuery. are you able to do this job. thanks

  • Adrian Green

    Handy snippet. However, it works more fully as the following:

    /* create a timeOut function in jQuery */
    jQuery.fn.idle = function(time){
    return this.each(function(){
    var i = $(this);
    i.queue(function(){
    setTimeout(function(){
    i.dequeue();
    }, time);
    });
    });
    };

  • http://james.revillini.com James Revillini

    This is brilliant! It should be in the core. I’m going to make the recommendation.

  • http://james.revillini.com James Revillini

    http://dev.jquery.com/ticket/4102 << referred them to your site in existing open enhancement request.

  • http://felipetonello.com felipe tonello

    Thank you man! It’s really helpful.

  • http://www.stockvirtual.com Patrick

    Very nice but I think you miss a return $(this); to be valid. Nice page!

  • http://www.ryancoughlin.com Ryan Coughlin

    Ah! Let me take a look. Did you add it right before the function closes?

  • http://www.paragarora.com Parag Arora

    Nice and thanks for sharing this. I am looking for continuous actions (continuous polling). i.e., show – > 2s -> hide -> 2s -> show. Is recursion the way to go. Will be great if someone can share a snippet on the recommended way.

  • http://www.hilalcelikhalat.com Serhan

    Looks helpful; I’m gonna try it on my site.. Hope it works perfect ;)

  • http://Yourwebsite... Zach

    And another thing lol:

    The code does not work…
    My element fades in but not back out
    I cut and pasted the code where it belongs and invoked it as such:
    $(“#d”).fadeIn().idle(5000).fadeOut();

    code above was:

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

    any help would be appreciated

  • http://Yourwebsite... salih0vicX

    Zach, try this – it worked for me:
    $(“#d”).fadeIn().idle(5000).fadeOut();
    $(“#d”).fadeOut();

    I suggest these articles: http://api.jquery.com/queue/ and http://api.jquery.com/jQuery.queue/

  • http://Yourwebsite... Onofrei George

    To complete the above comment:
    I was using it this way:

    $(this).idle(2000).click(ClickEvent);

    and the problem was that it couldn’t find anything to attach the click event after the idle event.

  • http://Yourwebsite... timmers

    @salih0vicX:

    that will use that original fadeout function that jquery uses.

    so what your telling Zach is that do this instead :

    $(“#d”).fadeIn();
    $(“#d”).fadeOut();

    so he won’t be using the new function that was posted above.
    I copied it too and I can’t get it to work.

  • Psivadasan

    Hi Ryan,

    How do I keep this function running? For example, after I fade out the element, I want to fade it back in, then fade out and so on.

    Thanks,

    Prasant