Open In App
Related Articles

jQuery delay() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

The delay() method is an inbuilt method in jQuery that 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.

Example 1: In this example, the timer is set to all the block. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    
    <!-- jQuery code to demonstrate delay method -->
    <script>
            $(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>


Output: 

 

Example 2: In this example, it has been shown how to delay an animation using this method. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    
    <!--jQuery code to show the working of delay method-->
    <script>
        $(document).ready(function () {
          
                $("#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>


Output: 

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 27 Oct, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials