jQuery API

.delay()

.delay( duration, [queueName] ) Returns: jQuery

Description: Set a timer to delay execution of subsequent items in the queue.

  • version added: 1.4.delay( duration, [queueName] )

    durationAn integer indicating the number of milliseconds to delay execution of the next item in the queue.

    queueNameA string containing the name of the queue. Defaults to fx, the standard effects queue.

Added to jQuery in version 1.4, the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively.

Using the standard effects queue, we can, for example, set an 800-millisecond delay between the .slideUp() and .fadeIn() of <div id="foo">:

$('#foo').slideUp(300).delay(800).fadeIn(400);

When this statement is executed, the element slides up for 300 milliseconds and then pauses for 800 milliseconds before fading in for 400 milliseconds.

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

Example:

Animate the hiding and showing of two divs, delaying the first before showing it.

<!DOCTYPE html>
<html>
<head>
  <style>
div { position: absolute; width: 60px; height: 60px; float: left; }
.first { background-color: #3f3; left: 0;}
.second { background-color: #33f; left: 80px;}
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<p><button>Run</button></p>
<div class="first"></div>
<div class="second"></div>
	
<script>
    $("button").click(function() {
      $("div.first").slideUp(300).delay(800).fadeIn(400);
      $("div.second").slideUp(300).fadeIn(400);
    });
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .delay() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .delay()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://rnikitin.ru Roman Nikitin

    And how we can execute own function after delay?
    Like:
    $.delay(1000, function()
    {
    alert(‘aloha!’);
    });
    Or we should use old style:
    setTimeout(function()
    {
    alert(‘aloha!’);
    }, 1000);

  • Anonymous

    Would be nice if one could apply delay/pause to .each() and similar iterators. For now I figured out this hack:
    //this will fade out and remove table rows one by one with 1/2sec intervals
    var i=0
    $(‘tr’).each(function(){
    $(this).delay(++i*500).fadeOut(‘slow’, function(){$(this).remove()} );
    })
    mainly, the secret is to set delay of each element at a different value.
    Has someone figured out a better way?

  • Anonymous

    You can always write your own function for doing so…

    This is what I’m using:

    $.fn.delay = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1}, time, callback);
    }

  • Anonymous

    Yes exactly. You should include this code, as you would with any other plugin for jQuery.

    With this plugin you’re still able to use delay the way the jQuery team intended it to be used:
    $(“#element”).animate().delay(300).animate();

    But you’re also able to have the count down to run a function once it hits zero:

    $(“#element”).delay(300, function(){
    $(this).animate();
    });

  • http://sites.google.com/site/jonnevermind/ Joη

    you know, you can use that “i” in the each function(i) like that and it function as your counter.

  • carlo

    Hy, I have a question:

    $(“#attesa”).delay(5000).hide(); // hide without delay
    $(“#attesa”).delay(5000).hide(1); // hide after delay

    why?

  • http://www.learningjquery.com/ Karl Swedberg

    When you use .hide() without a speed, it isn't part of the fx queue. Instead, it just hides the matched elements using display: none. You don't have to use .hide(1) though; .hide(0) works just as well.

    In any case, I think I should probably split up .hide() into two separate entries within http://api.jquery.com/hide/ rather than just two signatures.

  • chris

    if you don’t like any plugin workarounds try this

    $(xxx).animate({delay:1},DELAYMS, function() {
    delayedAction();
    });

    xxx = some element that does exist (eg body / div..)

  • Siege82

    Is there a way to cancel a delay? A bit like how you can use stop to cancel all queued events on an item.
    E.g,
    – on mouseenter set background color to red
    – on mouseleave wait 5 second (using .delay(5000)) then set background color to blue
    - if mouse reenters within the 5 seconds cancel the mouseleave event (using .stop) and cancel the delay (using ????)

    CODE
    ——
    $(‘#item’).bind(‘mouseenter’, function(){
    $(this).stop(true, true)
    .animate({
    backgroundColor : “#F00″
    }, 500);
    });

    $(‘#item’).bind(‘mouseenter’, function(){
    $(this).stop(true, true).delay(5000)
    .animate({
    backgroundColor : “#00F”
    }, 500);
    });

    .stop() doesn’t seem to work so what can be added to the ‘mouseenter’ event to cancel the delay on the ‘mouseleave’? I’d like to avoid the alternative option of faking a delay with an unnoticeable .animation() call. Although this would be cancelled by .stop() it defeats the object of having this new .delay() functionality.

  • Ben

    Have the same problem, I fade in an element after a specific event and fade it out after 3 sec. But the event can occur during the delay, so the fade out should be canceled.

    d
    .stop(true, true)
    .offset({ top: t, left: l })
    .animate({ opacity: “show”, top: t }, “slow”)
    .delay(3000)
    .animate({ opacity: “hide”, top: (t-10) }, “slow”);

    This does not work, but this does:

    d
    .stop(true, true)
    .offset({ top: t, left: l })
    .animate({ opacity: “show”, top: t }, “slow”)
    .animate({ delay:1 }, 3000, function() { // can’t use delay() here because it isn’t canceled by stop()
    d.animate({ opacity: “hide”, top: (t-10) }, “slow”);
    });

    Is this expected behaviour?

  • yoyo

    I find difficulties using jQuery delay function and with css display:none.
    I wrote a script to delay the event of hiding a thing using css(‘display’,'none’)
    Then I execute it.The element disappeared, but it is not delayed.
    (I wrote something like (‘xxx’).delay(2000).css(‘display’,'none’) but it happened without delay)

  • http://www.learningjquery.com/ Karl Swedberg

    See my comment below ( http://api.jquery.com/delay/#comment-31238561 )
    What I mentioned about .hide() applies to .css(‘display’, ‘none’) as well.

  • Anonymous

    .css() wil always happen straight away as it doesn’t go into the ‘fx’ queue. You therefore need to add it to a queue for it to be delayed like this:

    $(“body”).css({“background-color”: “#FF0000″})
    .delay(1000).queue(function () {$(this).css({“background-color”: “#ABC”});$(this).dequeue();})
    .delay(1000).queue(function () {$(this).css({“background-color”: “#FFFFFF”});$(this).dequeue();})

  • http://theneverstill.com/ TheNeverStill

    Another solution you could try if you are trying to delay the [display: none] until after the .animate() function is executed (or something similar) is to make use of the built-in callback function. For example:

    $(‘#slide’).animate({opacity: 0}, 525,’linear’, function () {$(this).css({display: “none”})});

    This will animate the opacity to 0 over the 525 then change display to none.

  • http://simplestick.com/ simplestick

    you don’t need to declare i as a public variable, you can merely assign an index to the function in the each method:

    $( ‘tr’ ).each( function( index )
    {
    $(this).delay( index *500 ).fadeOut(‘slow’, function(){$(this).remove()} );
    });

    I think that generally speaking this is the best/only way to sequence things with jQeury.

  • herrjuergen

    had the same problem and my solution was as simple as

    $(this).stop().fadeOut();

    maybe u have to clear the animationsqueue with .clearQueue() …

  • http://www.virtualcreations.de Virtual Creations

    Maybe someone did not realize this (I did not):

    1. .delay() cannot be stopped by using .stop()

    2. The only way to cancel a running delay is using .clearQueue().

  • anish

    is .delay() applicable only for visual effects – like fadeOut, hide() etc. ?

    can we apply .delay() to ajax functions?
    eg: $(‘#____’).html(___).delay(1000).load(_____) – will this work?

  • Russell Albin

    Here is what I am doing to achieve a 3 second pause, then fade out: $(‘#success’).delay(4000).fadeOut(4000);
    I hope that helps!

  • Russell Albin

    Sorry, I meant a 4 second pause, not 3 second pause, then a 3 second fade out effect.

  • Simon_mudd19

    is there any way of making a bigger pause? I am looking to make a 30 second pause but it still registers as 3 seconds and it doesnt matter how many 0's you put on the end.

  • Zequez

    You can try stacking multiple delays

  • domin

    I want to ask the same question.
    $('form').delay(1000);
    It's not working..

  • Suit

    Does it work like http://docs.jquery.com/Cookbook/wait or is it different?

  • Howiefang

    Try put things in the callback func of a “can't see”visual effect.

  • Jared

    I would love to see the ability to have a callback for delay. So we could do something like:

    $.delay(1000, function(){
    //Do anything after 1 second
    });

  • Rybazrakom

    I'm want insert “delay” before .attr() this possible? if this impossible with “delay”, where searshed?

  • Hashan Gayasri

    you can use
    var t = setTimeout ( “$().anything”, delaytime );

  • http://kwags.myopenid.com/ KWags

    then just use fadeOut(1) ?

  • Karen

    How I can do smth like this?
    $(‘.foo’).slideUp(300).delay(300).remove();

  • http://www.learningjquery.com/ Karl Swedberg

    You could do something like this:

    $('.foo').slideUp(300).delay(300).queue(function() {
    $(this).remove();
    });

    See queue for more information.

  • Israel

    $(document).ready(function() {

    $('#slides').css({'background':'url(“images/1.jpg”)'});
    $.delay(2000,function(){$('#slides').css({'background':'url(“images/2.jpg”)'});});

    });
    why is it not working :(

  • Gov

    $('#SlideShow').replaceWith('<img id=”SlideShow” src=”img02.png”>').delay(2000);

    delay of 2sec won't work, what am I dong wrong?

  • http://twitter.com/nikospkrk Nicolas Armstrong

    It's only the order you're doing wrong. if you want the delay to be before your replaceWith (btw you'd better do a .html()), you have to place your .delay() before.
    Like : $('#SlideShow').delay(2000).html('<img id=”SlideShow” src=”img02.png”>');

  • Christian Gnoth

    i am too looking for a solution of that. i would like to do a .post() call after a delay(). is there a documentation of other queues? maybe a queue for the .post and .load calls?

  • Christian Gnoth

    what type of other queues are there? is there a queue for a .post() call?

  • n0aX

    $.doTimeout(interval,function() {

    });

    check benalman's page @ http://benalman.com/projects/jquery-dotimeout-plugin/

  • Man361708964

    我晕

  • Man361708964

    我晕

  • Man361708964

    我晕

  • Man361708964

    我晕

  • huber

    Plesase help me, because i have no idea why this's not working:

    $('.animation').delay(500).css('background-position', posx+'px 0px');

  • http://pulse.yahoo.com/_3O4AZXMRGJLNOPTONZQPJFRP7A Rain ☂

    i think u can do like this .delay(3000*10)

  • Xyx

    no

  • http://websitecenter.ca/ Montreal Web Design

    Is there setTimeout wrapper for jquery?

  • Jervie_10

    You can use this code
    $(“#xxx”).css({zIndex:”-5″});

  • http://www.facebook.com/people/Clister-Nitish-Dmello/100000369155324 Clister Nitish Dmello

    I want to toggle the background of “a” tag with a delay.

    $(“#para>a”).css('background-image','url(“btn_bigtext_down1.png”)');
    $(“span”).slideUp(3300);
    $(“#para>a”).css('background-image','url(“btn_bigtext_up1.png”)');

    above code isnt working fine,PLZ help