Simple examples of JS callback functions and js callback Functions
The callback function of JS is very simple. Check the Code:
In a. js
Var myback = null; function load (obj) {myback = obj;} function save () {// background request $. ajax. post ...... // call the callback function myback ();}
Call in B. js
// Pass the function to be called back as a parameter! Load (function () {this. hide ();})
How to Write a callback function with parameters in js? It is better to write an example.
I guess, because js functions can have arbitrary parameters (the parameters during function declaration can be different from the number of parameters written during call ):
Work_DataManager_AutoCompare_List.GetComparePageStatusCount (pRand_id, function (response, trObj)
{
TrObj ...... Yes?
}
How to Implement Asynchronous js callback functions?
What about Asynchronization?
When it takes a long time for a function to be executed, JS will asynchronously execute the next function, instead of waiting until the current function is executed before executing the following function.
Callback function:
The callback function is used to prevent the program from executing the following functions before a function is executed, because the following functions may require the return values of the above functions, so the callback function came out.
In fact, callback is also a function. If this callback function still takes a long time, it will generate an asynchronous function. Therefore, if the callback function has been executed for too long, JS will directly execute the following function without executing the callback function. So I can understand it ~ This example is used.
Settimeout for demonstration ~ You can use latency to replace the waiting time (using ajax as an example )~
(Function () {$. ajax ({url: "123. json ", success: function (msg) {setTimeout (alert (msg), 3000) ;}}) the callback function is executed asynchronously ~ The execution will pop up first, and then the content of msg will pop up ~