jQuery -

Delay


What is jQuery Delay?

The jQuery .delay() method sets a delay for the next animation in the queue. This method is useful for creating timed sequences of animations. This tutorial covers various aspects of using the .delay() method with detailed examples, explanations, and useful tips.


1. Basic Delay

You can use the .delay() method to add a delay before the next animation starts.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayButton").click(function(){
                $("div").delay(1000).slideUp();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="delayButton">Delay Slide Up</button>
</body>
</html>
        
    

This example demonstrates how to delay the start of a slide-up animation.


2. Delay with Multiple Animations

You can chain multiple animations with delays between them to create complex sequences.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Multiple Animations</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayButton").click(function(){
                $("div").slideUp().delay(1000).slideDown().delay(1000).fadeOut();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="delayButton">Start Multiple Animations</button>
</body>
</html>
        
    

This example shows how to delay the start of multiple animations in sequence.


3. Delay with Fade

You can combine the .delay() method with fading methods like .fadeIn() and .fadeOut().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Fade</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayButton").click(function(){
                $("div").delay(1000).fadeIn();
            });
        });
    </script>
</head>
<body>
    <div style="display:none; background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="delayButton">Delay Fade In</button>
</body>
</html>
        
    

This example demonstrates how to delay the start of a fade-in animation.



4. Delay with Slide

You can combine the .delay() method with sliding methods like .slideUp() and .slideDown().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Slide</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayButton").click(function(){
                $("div").delay(1000).slideDown();
            });
        });
    </script>
</head>
<body>
    <div style="display:none; background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="delayButton">Delay Slide Down</button>
</body>
</html>
        
    

This example shows how to delay the start of a slide-down animation.


5. Delay with Custom Animations

You can use the .delay() method with custom animations created using the .animate() method.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Custom Animations</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayButton").click(function(){
                $("div").delay(1000).animate({left: "250px"});
            });
        });
    </script>
    <style>
        div {position: relative; background-color: yellow; width: 100px; height: 100px;}
    </style>
</head>
<body>
    <div>This is a div element.</div>
    <button id="delayButton">Delay Custom Animation</button>
</body>
</html>
        
    

This example demonstrates how to delay the start of a custom animation.


6. Delay with Toggle

You can use the .delay() method with toggle animations to delay the toggle effect.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Toggle</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#toggleButton").click(function(){
                $("div").delay(1000).toggle("slow");
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="toggleButton">Delay Toggle</button>
</body>
</html>
        
    

This example shows how to delay the start of a toggle animation.


7. Delay with Chaining

You can chain multiple animations with delays between them for complex sequences.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Chaining</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#chainingButton").click(function(){
                $("div").slideUp().delay(1000).slideDown().delay(1000).fadeOut();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="chainingButton">Chain Animations with Delay</button>
</body>
</html>
        
    

This example demonstrates how to chain animations with delays to create a sequence.



8. Delay with Ajax

You can use the .delay() method with Ajax calls to delay actions after an Ajax request.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Ajax</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#ajaxButton").click(function(){
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/posts/1",
                    success: function(result){
                        $("div").html(result.title).delay(1000).slideDown("slow");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <button id="ajaxButton">Load Data and Delay</button>
    <div style="display:none; background-color: yellow; padding: 10px;"></div>
</body>
</html>
        
    

This example demonstrates how to delay the display of content after an Ajax request.


9. Delay with Event Handling

You can use the .delay() method within event handlers to delay actions in response to events.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Event Handling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#eventButton").click(function(){
                $("div").delay(1000).slideUp();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="eventButton">Delay Slide Up on Click</button>
</body>
</html>
        
    

This example shows how to delay an animation triggered by a click event.


10. Delay with Promises

You can use the .delay() method with jQuery promises to manage complex asynchronous operations.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .delay() Method - Promises</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#promiseButton").click(function(){
                $("div").slideUp().delay(1000).promise().done(function(){
                    $(this).slideDown().delay(1000).promise().done(function(){
                        $(this).fadeOut();
                    });
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="promiseButton">Chain with Promises</button>
</body>
</html>
        
    

This example demonstrates how to use the .delay() method with jQuery promises for complex sequences.