jQuery delay() Call a Function After Some Time

Introduction

You can use the jQuery delay() method to call a function after waiting for some time.

Pass an integer value to this function to set the time interval for the delay in milliseconds.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Calling a Function after Specific Time Period in jQuery</title>
<style>
    img{// w  ww.  j a va2s  .  c o  m
        display: none;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.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="image5.png" alt="image">
</body>
</html>



PreviousNext

Related