What is a callback function?

前端 未结 21 2411
耶瑟儿~
耶瑟儿~ 2020-11-21 23:01

What is a callback function?

相关标签:
21条回答
  • 2020-11-21 23:41

    Callback Function A function which passed to another function as an argument.

    function test_function(){       
     alert("Hello world");  
    } 
    
    setTimeout(test_function, 2000);
    

    Note: In above example test_function used as an argument for setTimeout function.

    0 讨论(0)
  • 2020-11-21 23:42

    A callback function is one that should be called when a certain condition is met. Instead of being called immediately, the callback function is called at a certain point in the future.

    Typically it is used when a task is being started that will finish asynchronously (ie will finish some time after the calling function has returned).

    For example, a function to request a webpage might require its caller to provide a callback function that will be called when the webpage has finished downloading.

    0 讨论(0)
  • 2020-11-21 23:42

    A callback function is a function you specify to an existing function/method, to be invoked when an action is completed, requires additional processing, etc.

    In Javascript, or more specifically jQuery, for example, you can specify a callback argument to be called when an animation has finished.

    In PHP, the preg_replace_callback() function allows you to provide a function that will be called when the regular expression is matched, passing the string(s) matched as arguments.

    0 讨论(0)
  • 2020-11-21 23:42

    look at the image :)this is how it works

    Main program calls library function (which might be system level function also) with callback function name. This callback function might be implemented in multiple way. The main program choose one callback as per requirement.

    Finally, the library function calls the callback function during execution.

    0 讨论(0)
  • 2020-11-21 23:43

    Callbacks are most easily described in terms of the telephone system. A function call is analogous to calling someone on a telephone, asking her a question, getting an answer, and hanging up; adding a callback changes the analogy so that after asking her a question, you also give her your name and number so she can call you back with the answer.

    -- Paul Jakubik, "Callback Implementations in C++"

    0 讨论(0)
  • 2020-11-21 23:43

    What is callback?

    • In general, a telephone call made to return one that someone has received.
    • In computing, a callback is a piece of executable code that is passed as an argument to other code. When the function is done with its work (or when some event occurred), it calls your callback (it calls you back - hence the name) function.

    What is a callback function?

    • a callback function is like a Servant who "calls back" to his Master when he has completed a task.
    • a callback function is a function that is passed to another function (let's call this other function otherFunction) as a parameter, and the callback function is called (or executed) inside the otherFunction.
        function action(x, y, callback) {
            return callback(x, y);
        }
    
        function multiplication(x, y) {
            return x * y;
        }
    
        function addition(x, y) {
            return x + y;
        }
    
        alert(action(10, 10, multiplication)); // output: 100
    
        alert(action(10, 10, addition)); // output: 20
    

    In SOA, callback allows the Plugin Modules to access services from the container/environment.

    Analogy: Callbacks. Asynchronous. Non-blocking
    Real life example for callback

    0 讨论(0)
提交回复
热议问题