Q:

How to call a function automatically after waiting for some time in jQuery

0

How to call a function automatically after waiting for some time in jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery delay() method

You can use the jQuery delay() method to call a function after waiting for some time. Simply pass an integer value to this function to set the time interval for the delay in milliseconds.

Let's check out an example to understand how this method basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Execute a Function after Certain Time</title>
<style>
    img{
        display: none;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
function showImage(){
    $("img").fadeIn(500);
}
$(document).ready(function(){ 
    $(".show-image").click(function(){
        $(this).text('loading...').delay(1000).queue(function() {
            $(this).hide();
            showImage(); //calling showimage() function
            $(this).dequeue();
        });        
    });
});
</script>
</head>
<body>
    <button type="button" class="show-image">Show Image</button>
    <img src="../images/kites.jpg" alt="Flying Kites">
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to add CSS properties to an element dynamicall... >>
<< How to define a function in JavaScript...