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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 419
    Comment on it

    Callback function in Jquery:

        Callback function is a function which is executed after the current function (i.e the currently running function) has been executed.
        As the name signifies Callback function is called back at some specified point inside the containing functions body, it is not executed immediately.

    Syntax:

    $(selector).hide(speed,callback);
    

    Let us understand it more clearly with the help of an example:
        In javascript the execution takes place line by line, even then the next line gets executed before the effect of first line is completed, which can create an error.

    Example:

    $("button").click(function(){
        $("p").hide(1000);
        alert("The paragraph is now hidden");
    });
    

        In the above example on click of the button the paragraph is hidden. But before the paragraph is hidden, the alert function is executed, i.e the paragraph is hidden after the alert function is executed, to overcome this callback functions are introduced.

    Example:

    $("button").click(function(){
        $("p").hide("slow", function(){
            alert("The paragraph is now hidden");
        });
    });
    

        In this above example alert function is declared inside the callback function which is passed as a parameter in the hide function. Therefore the alert function will only be executed after the hide function has finished its execution (i.e. after the paragraph is hidden).

    Now as we are familiar with the callback functions, let us know more about them:

        A callback function is also known as a higher-order function. It is a function that is passed to another function as a parameter and gets executed inside the other function.When we pass a callback function as an argument to another function,this doesn't means that we are executing the function,we are only passing the function definition. This can be verified by the fact that when we are passing the function we are not passing it with the trailing pair of executing parenthesis '()' like we do when we are executing a function.

 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: