JS callback function (callback) understanding

Source: Internet
Author: User

mark!

JS Learning

Do not like JS, but like jquery, do not explain.

When self-study jquery, saw an English word (Callback), immediately back faint cold sweat. Google quickly found that the original Chinese translation into a callback. That is, the callback function. Do not understand Ah, so in the Google callback function, found that the online Chinese interpretation is too "abstruse", I admit that I caishuxueqian. After looking at some examples of callbacks, it seems a bit understandable. Here is my understanding of the callback function, if the understanding is wrong, please correct, not very grateful.

First of all, from the English definition on the jquery website, as a Chinese, I really feel tragic. The definition of a callback by the domestic "master" interpretation of what, in that circle, it seems that only to put you around, he is a master. Clouds, everything is a cloud.

A callback is a function so is passed as an argument to another function and are executed after it parent function has C ompleted.

This is the explanation of JS, I did not say in other languages.

The literal understanding is that the callback is the process of invoking a function. So let's start by understanding the invocation process. Function A has a parameter, which is a function B, and executes function B after function a finishes executing. Then this process is called a callback.

In fact, Chinese is also very good understanding: callback, callback, is called back to the meaning. Function A is done in advance, and then the function B is called back.

Take a realistic example: after the date you send your girlfriend home, parting, you will certainly say: "Home to give me clockwork information, I am very worried about you." "No, then your girlfriend really sent you a message when she came home. Boy, you have a chance.

In fact, this is the process of a callback. You left a function B (ask your girlfriend to give you A clockwork message) to your girlfriend, and then your girlfriend goes home, and the action to go home is function A. She had to get home first, the content of function A is finished, then function B is executed, and then you receive a message.

It is important to be clear here: function B is passed to function A as a parameter, then function B is called a callback function.

Perhaps some people have doubts: must be passed in the form of parameters, I can not directly in function A call function B? I can do that. In the solution.

<: If you call it directly in function A, the callback function is limited to death. But using the function to do the argument has the following advantages: When you have a (b) function B is a callback function, and you can also a (c) this time, function c is a callback function. If you write function a () {...; b ();} The flexibility of the variable is lost. >

The following code is used to confirm my understanding.

Reprint: http://luxiao1223.blog.51cto.com/2369118/482885

A simple example of passing parameters to a JavaScript callback function

Problem:

Now there are functions defined as follows:
function A (a,callback) {
....
}
function B () {
....
}
You can have the following call
A (A, B) to implement the callback.


Now I want to pass a parameter C to B, that is, the implementation is similar to:

A (A, B (c)), the effect of the experts to help you, how should be achieved?

Answer:

      1. <title>
      2. Jsp3
      3. </title>
      4. <body bgcolor= "#ffffff" >
      5. <script language= "javascript" type= "Text/javascript" >
      6. function A (a,callback) {
      7. var b=callback;
      8. alert (A+B);
      9. }
      10. function B (c) {
      11. Return (-C);
      12. }
      13. </script>
      14. <form method= "POST" action= "jsp3.jsp" >
      15. <br><br>
      16. <input type= "button" name= "Submit" value= "Submit" onclick= "A (4,b (3));" >
      17. </form>
      18. </body>

Reproduced in: http://blog.sina.com.cn/s/blog_4bb52a160100da4w.html

<span style= "FONT-SIZE:14PX;" > I wrapped the jquery Ajax method function Doajax (u,param,callback) {      $.ajax ({            type: ' POST ',            url:u,            Data:param ,            Success:callback      });} function Showalert (data) {     alert (data);} such as this call Doajax ("server.php", "id=12&type=1", Showalert), $.ajax in success, will return a data to Showalert, display, no problem. But, How do I write when I want to pass a parameter to Showalert? written as Doajax ("server.php", "id=12&type=1", Showalert ("Hi", "data"), or Success:callback Written Success:callback (Msg,data) obviously not, alternative? Help ~~~</span>

  

<span style= "Font-size:14px;color: #333333;" > Simple, many methods define the callback function, the callback function is also a function, that is, regardless of how to pass, only need to be a function type. The wording is as follows. Mode 1,doajax (parameter 1, parameter 2,function (request,opts) {         callback (REQUEST,OPTS,AGRS);}); function callback (Request,opts,args) {              }; mode 2,var args=n;doajax (parameter 1, parameter 2,function (request,opts) {       var x=n;       Callback function code block       : And above almost the same, look at the personal coding options.      }); </span>

  

The more you don't want to learn, the more you get tortured. Look at the JavaScript callback. Directly on the code bar.

    1. <</span>html>
    2. <</span>head>
    3. <</span>title> callback Function (callback) </</span>title>
    4. <</span>script language= "javascript" type= "Text/javascript" >
    5. Function A (callback) {
    6. Alert ("I come from parent function a");
    7. Callback ();
    8. }
    9. Function B () {
    10. Alert ("I came from child (callback) function B");
    11. }
    12. Function C () {
    13. Alert ("I'm from the child (callback) function C");
    14. }
    15. function Test () {
    16. A (b);
    17. A (c);
    18. }
    19. --------------------the following 2 functions simply demonstrate the passing of a parent function to a callback function----------
    20. Callback is a parameter name. This parameter represents a function for the moment. This function has no arguments
    21. 。。。 There should be other formulations, right?
    22. function e (m,n,callback) {
    23. var d = m+n;
    24. Alert ("A parameter generated from the parent function e will be passed to the callback function, this parameter is:" +d);
    25. This is where you write the function you want to call---parameters to be correct
    26. Callback (d);
    27. //-----------
    28. }//     |
    29. //      |
    30. function callback (data) {
    31. Alert ("I am the callback function, my name is: callback, I receive the parameters from the parent function, the parameters are:" +data);
    32. }
    33. </</span>script>
    34. </</span>head>
    35. <</span>body>
    36. <</span>h2>a CallBack is a function this is passed as an argument to another function and are executed after it s parent function has completed;</</span>h2>
    37. <</span>hr/>
    38. <</span>a href= "#" onclick= "Javascript:test ();" > callback</</span>a><</span>br/> of non-pass parameters
    39. <</span>br/>
    40. <</span>br/>
    41. <</span>br/>
    42. <</span>a href= "#" onclick= "Javascript:e (7,8,callback);" > callback</</span>a> of transfer parameters
    43. <</span>hr/>
    44. </</span>body>
    45. </</span>html>

Reproduced in: http://blog.sina.com.cn/s/blog_601b97ee0101f1rc.html

JS callback function (callback) understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.