JS callback function

Source: Internet
Author: User

When using jquery, use the concept of callback (), the callback function. and a lot.

Like what:
$.ajax ({    URL: "Test.json",    Type: "GET",    data: {username:$ ("#username"). Val ()},    DataType: "JSON",    beforsend:function () {          //disable button prevents duplicate commit        $ ("#submit"). attr ({disabled: "Disabled"})    , complete     : Function (msg) {         //The callback function called after the request is completed (called when the request succeeds or fails)    }, the     error:function (msg) {         ///request fails when the call is called     },     sucess:function (msg) {         //callback function called after successful request     }});

callback function everyone will use, but jquery encapsulation, you can not let everyone understand the real use of the callback function. The JS Api explains this: a callback is a function, which is passed as an argument to another function and are executed after its parent F Unction has completed. Of course we can actually try out the magic of the callback function in JS. 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 is the code:As you can see, I wrote jquery's Ajax functions in a (callback), B (), C () method, and note that this is the function of Ajax. There is a difference, I call the built-in function separately, and my own defined function. The difference is here, because jquery and JS use the same callback, so in this case, jquery's built-in function will be called. but the function that you define is not recognized, even if it is not called.(after this paragraph is not very understanding, hope to see the master please analyze)
so pay attention to this at the time of the callback. http://www.cnblogs.com/xcsn/archive/2013/01/03/2843218.html

Function A (callback) {
...
var record = Reallycomplexprocedure (); After a series of complicated operations, I got the data record.
if (typeof callback = = = = "function") {
Callback (record);
}
}
function B (data) {
...//Processing of data
}
A (b);

JS callback function

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.