The callback function in JS, what is a callback function?

Source: Internet
Author: User

I. function of the callback function

The JS code will go to the top and the next line goes on, but sometimes we need to wait until the end of an operation to do the next operation, and then we need to use the callback function.

two . explanation of the callback function

because the function is actually an object, that is built-in objects , it can be stored in a variable, passed to another function by argument, we can pass it as an argument to another function, execute it in a function, and even return it after execution . It has always been considered by " Professional programmers " as a difficult technology to understand.

The English explanation of the callback function is:

A callback is a function so is passed as an argument to another function and are executed after it parent function has C ompleted.

The callback function is a function that is passed as a variable to another function, which executes after the principal function has finished executing.

Function A has a parameter function b,function B will be in function A Execute is called after execution is complete.

First define two function

function A (callbackfunction) {
Alert (" This is the parent function a");
var m = 1;
var n=3;
Return callbackfunction (m,n);//function a after execution, get m,n Two variables, function B uses these two variables as parameters to execute, after which the callback function is returned
}

Function B (m,n) {
Alert (" this is callback function B");
return m+n;
}

$ (function () {
var result = a (b);//here Function B is passed as a variable to function a
Alert ("result =" + result);
});

The order of execution is:

This is the parent function a

This is the callback function B

result = 4

The function first executes the topic function a, calls the callback function b, and returns The return value of function A.

1. Click on the new follow-up list, then pop up a modal. Passed a Cusid variable and Switchcus method in modal. Modal is originally on this $ (document), so the CUSID variable and Switchcus method can be used directly in the modal.

In the open modal, click on the previous next, execute the method Switchcus from the Customerlist.js page over there. and passed a callback function to the Switchcus method as a parameter. Then, after the Customerlist.js page executes the Switchcus function, it takes IDArray as the parameter of the callback function, and then executes the callback function in the modal.

The callback function in JS, what is a 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.