jQuery | delay() with Examples

The delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue.
Syntax:

$(selector).delay(para1, para2);

Parameter: It accepts two parameters which are specified below-

  • para1: It specifies the speed of the delay.
  • para2: It is optional and specifies the name of the queue.

Return Value: It returns the selected element with the specified speed.

jQuery code to show the working of delay() method:

Code #1:
In the below code, timer is set to all the block.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
                jquery/3.3.1/jquery.min.js"></script>
    <script>
        <!-- jquery code to demonstrate delay method -->
        $(document).ready(function() {
            $("button").click(function() {
                $("#d1").delay("slow").fadeIn();
                $("#d2").delay("fast").fadeIn();
                $("#d3").delay(1000).fadeIn();
                $("#d4").delay(4000).fadeIn();
            });
        });
    </script>
</head>
  
<body>
    <!-- click on this button -->
    <button>Click Me!</button>
    <br>
    <br>
    <div id="d1" style="width:50px;height:50px;display:
                 none;background-color:lightgreen;"></div>
    <br>
    <div id="d2" style="width:50px;height:50px;display:
                 none;background-color:green;"></div>
    <br>
    <div id="d3" style="width:50px;height:50px;display:
                 none;background-color:orange;"></div>
    <br>
    <div id="d4" style="width:50px;height:50px;display:
                 none;background-color:yellow;"></div>
    <br>
</body>
  
</html>

chevron_right


Output:
Before clicking the “Click Me” button-

After clicking the “Click Me” button-

Code #2:
In the below code, It has been shown that how to delay an animation using this method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
                jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
        <!-- jQuery code to show the working of delay method -->
            $("#btn1").click(function() {
                $("div").animate({
                    height: "150px"
                });
                $("div").animate({
                    width: "150px"
                });
                $("div").delay(1200).animate({
                    width: "300px",
                    padding: "30px"
                });
            });
        });
    </script>
    <style>
        #d {
            display: block;
            width: 200px;
            height: 100px;
            background-color: green;
            font-size: 30px;
            padding: 10px;
        }
    </style>
</head>
  
<body>
    <div id="d">GeeksforGeeks !</div>
<!--click on this button to show the effect of delay method -->
    <p>
        <button id="btn1">Click Me!</button>
    </p>
</body>
  
</html>

chevron_right


Output:
Before clicking the “Click Me” button-

After clicking the “Click Me” button-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.