Simple examples of JS callback functions and js callback Functions

Source: Internet
Author: User

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 ~

 

Related Article

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.