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.

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.

jQuery.delay() is best for delaying between queued jQuery effects and such, and 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 { width: 60px; height: 60px; float: left; }
.first { background-color: #3f3; }
.second { background-color: #33f;}
</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:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .delay() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • If you really need to .stop() a .delay(), I recommend using .animate({g:1}, 3000) instead of .delay(3000). This is how you used to trick a delay before .delay(), and since it is an animation, it'll stop properly. g is just a garbage variable.
  • billonthebeach
    Just beginning with Jquery and so far its fabulous!

    A basic question with what would appear to be an easy answer:

    Does the browser continue other operations during the delay or does the browser freeze until the delay is concluded and the script progresses?

    thanks for your prompt response.

    bill
  • Sebastian
    How does delay(0) work? Does it postpone effect execution till all the events are dispatched? If I do .fadeout(xx).delay(0) in 'blur', can I cancel the effect in following events before it starts? Should I use stop() or clearQueue()?
  • 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)
  • See my comment below ( http://api.jquery.com/delay/#comment-31238561 )
    What I mentioned about .hide() applies to .css('display', 'none') as well.
  • 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?
  • carlo
    Hy, I have a question:

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

    why?
  • 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.
  • mike nelson
    excellent idea, thanks