jQuery delay()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery delay()

The jQuery delay() method is used to delay the execution of functions in the queue. It is a best method to make a delay between the queued jQuery effects. The jQUery delay () method sets a timer to delay the execution of the next item in the queue.

Syntax:

  1. $(selector).delay (speed, queueName)   

speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast and milliseconds.

queueName: It is also an optional parameter. It specifies the name of the queue. Its default value is "fx" the standard queue effect.

Let's take an example to see the delay effect:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
  5. <script>    
  6. $(document).ready(function(){    
  7.   $("button").click(function(){    
  8.     $("#div1").delay("slow").fadeIn();    
  9. });    
  10. });    
  11. </script>    
  12. </head>    
  13. <body>    
  14. <button>Click me</button><br>  
  15. <div id="div1" style="width:90px;height:90px;display:none;background-color:black;"></div><br>    
  16. </body>    
  17. </html>    

Test it Now

Output:

Click me

jQuery delay() example with different values

Let's see a jQuery delay() effect example where we are using fast, slow and milliseconds values.

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
  5. <script>    
  6. $(document).ready(function(){    
  7.   $("button").click(function(){    
  8.     $("#div1").delay("fast").fadeIn();    
  9.     $("#div2").delay("slow").fadeIn();    
  10.     $("#div3").delay(1000).fadeIn();    
  11.     $("#div4").delay(2000).fadeIn();    
  12.     $("#div5").delay(4000).fadeIn();    
  13. });    
  14. });    
  15. </script>    
  16. </head>    
  17. <body>    
  18. <p>This example sets different speed values for the delay() method.</p>    
  19. <button>Click to fade in boxes with a different delay time</button>    
  20. <br><br>    
  21. <div id="div1" style="width:90px;height:90px;display:none;background-color:black;"></div><br>    
  22. <div id="div2" style="width:90px;height:90px;display:none;background-color:green;"></div><br>    
  23. <div id="div3" style="width:90px;height:90px;display:none;background-color:blue;"></div><br>    
  24. <div id="div4" style="width:90px;height:90px;display:none;background-color:red;"></div><br>    
  25. <div id="div5" style="width:90px;height:90px;display:none;background-color:purple;"></div><br>    
  26. </body>    
  27. </html>    

Test it Now

Output:

This example sets different speed values for the delay() method.

Click to fade in boxes with a different delay time