Callback in JS (used when blocking synchronous or asynchronous)

Source: Internet
Author: User

1, the callback is a function of the call process, function A has a parameter, this parameter is a function B, when function a executes after the execution of function B,

Then this process is called a callback.

eg.

function A (callback) {   alert (' parent ');   Alert (' callback ');   Callback ();} function B () {   alert (' B ');} A (b);

Operation Result:

Parent

Callback

B

2. When will the callback be executed?

callback functions are typically executed at the end of the synchronization scenario and are not executed in an asynchronous situation because the event is not triggered or

Condition not satisfied

3, the callback function uses the occasion

(1) Resource loading: Execute callback after dynamically loading JS file, execute callback after loading IFRAME, Ajax operation Callback, picture loading complete

Execute callbacks, etc.

(2) Dom events and node. JS events based on callback mechanism

(3) SetTimeout delay time is 0, this hack is often used, settimeout call function is actually a callback

The embodiment

(4) Chained calls

Callback in JS (used when blocking synchronous or asynchronous)

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.