JavaScript callback function those things ~

Source: Internet
Author: User
Tags setinterval

What is a callback function?

A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs , and is used to respond to the event or condition .

1. Using callback functions for asynchronous operations ~

// Example function Add (NUM1, num2, callback) {    var sum = num1 + num2;    Callback (sum);} function print (num) {    console.log (num);}

Note: When a call takes a long time to cause the entire execution queue to wait for the execution result and the block cannot proceed, such as the ability to pass an asynchronous callback, the primary execution queue can proceed, and after the time-consuming call executes, the callback function is called to notify the main execution queue to take back the execution result. Further processing .....

2. We can also use callback functions to perform synchronous operations.

// The purpose is to execute FUNC2 after the FUNC1 code execution is complete.  var func1=function(callback) {    //dosomething.    typeof (callback) = = = "function") && callback ();} Func1 (FUNC2);     var func2=function() {}
Usage scenarios for callback functions

1. Resource loading: Execute callback after dynamically loading JS file, execute callback after loading IFRAME, Ajax operation Callback, image loading complete execution callback, Ajax and so on. Common
2. DOM events and node. JS events are based on a callback mechanism (the node. JS callback may have multiple layers of callback nesting issues).
3. SetTimeout delay Time is 0, this hack is often used, settimeout call function is actually a callback embodiment
4. Chained invocation: When chaining calls, it is easy to implement chained calls in the evaluator (setter) method (or in a method that does not have a return value in itself), and the accessor (getter) is relatively bad for chaining calls because you need the accessor to return the data you need instead of the this pointer. If you want to implement a chained method, you can use a callback function to implement
5. SetTimeout, SetInterval's function call gets its return value. Since all two functions are asynchronous, that is, their call timing and the program's main process is relatively independent, so there is no way to wait for their return value in the body, they are opened when the program will not stop waiting, otherwise it will lose the meaning of settimeout and setinterval, So with return there is no point, only using callback. The meaning of callback is to notify the agent function of the result of the timer execution to deal with it in time.

JavaScript callback function those things ~

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.