← Back to Chapters

jQuery delay() Method

⏱️ jQuery delay() Method

? Quick Overview

The delay() method in jQuery is used to pause the execution of animations or effects for a specified amount of time before continuing.

? Key Concepts

  • Works only with jQuery effects and animations
  • Time is specified in milliseconds
  • Queued with other animation methods

? Syntax / Theory

The delay() method inserts a delay in the animation queue.

Syntax: $(selector).delay(time)

? Code Example(s)

? View Code Example
// Delay hiding the box by 2 seconds
$(document).ready(function(){
$("#btn").click(function(){
$("#box").delay(2000).fadeOut();
});
});

?️ Live Output / Explanation

What happens?

When the button is clicked, the box waits for 2 seconds and then smoothly fades out.

? Interactive Example

 
? View Code Example
// Interactive delay animation example
$("#btn").on("click",function(){
$("#box").delay(1000).slideUp().delay(1000).slideDown();
});

? Use Cases

  • Creating step-by-step animations
  • Adding pauses between UI effects
  • Improving user experience with timed transitions

✅ Tips & Best Practices

  • Use delay() only with animations
  • Keep delays short to avoid poor UX
  • Chain with effects like fadeIn() or slideUp()

? Try It Yourself

  • Change the delay time and observe behavior
  • Chain multiple delays with different effects
  • Combine delay with animate()