Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Callback Function in jQuery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 396
    Comment on it

    Callback Functions:

    It is used to prevent executing further code until earlier effects has not executed completely. As we know in jQuery effects takes time to complete and sometime next line code get executed while the earlier effects are still executing.

    Example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Callback Functions</title>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("p").slideToggle("slow", function(){
                alert(" Effect  completed successfully.");
            });
        });   
    });
    </script>
    </head>
    <body>
    	<div class="container">
    	    <p>Demo of Callback Function in jQuery</p>
    	    <div style="text-align:center;"><button type="button">Click me</button></div>
    	</div>
    </body>
    </html> 

    In the above example we used alert() statement inside callback function for slideToggle() method. And when we run the above code the message written inside alert() statement will be displayed only when the slide toggle effect has completed.

     

    You can check the output of above example here: https://jsfiddle.net/7abbqqyf/1/

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: