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:
$(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:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").delay("slow").fadeIn(); }); }); </script> </head> <body> <button>Click me</button><br> <div id="div1" style="width:90px;height:90px;display:none;background-color:black;"></div><br> </body> </html>
Output:
Let's see a jQuery delay() effect example where we are using fast, slow and milliseconds values.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").delay("fast").fadeIn(); $("#div2").delay("slow").fadeIn(); $("#div3").delay(1000).fadeIn(); $("#div4").delay(2000).fadeIn(); $("#div5").delay(4000).fadeIn(); }); }); </script> </head> <body> <p>This example sets different speed values for the delay() method.</p> <button>Click to fade in boxes with a different delay time</button> <br><br> <div id="div1" style="width:90px;height:90px;display:none;background-color:black;"></div><br> <div id="div2" style="width:90px;height:90px;display:none;background-color:green;"></div><br> <div id="div3" style="width:90px;height:90px;display:none;background-color:blue;"></div><br> <div id="div4" style="width:90px;height:90px;display:none;background-color:red;"></div><br> <div id="div5" style="width:90px;height:90px;display:none;background-color:purple;"></div><br> </body> </html>
Output:
This example sets different speed values for the delay() method.