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