JavaScript Asynchronous Programming method------"callback function"
This is the most basic method of asynchronous programming.
Assume that there are two functions F1 and F2, which wait for the result of the former execution.
F1 ();
F2 ();
If F1 is a time-consuming task, consider rewriting F1 and writing F2 as the F1 callback function.
Function F1 (callback) {SetTimeout (function () {//F1 task code callback ();
}, 1000);
}
The execution code becomes the following:
F1 (F2);
In this way, we turn the synchronous operation into an asynchronous operation, and the F1 does not clog the program, which is equivalent to executing the main logic of the program and delaying the execution of the time-consuming operation.
The advantages of the callback function are simple, easy to understand and deploy, the disadvantage is that it is not conducive to the reading and maintenance of the code, highly coupled between the parts (coupling), the process will be confusing, and each task can only specify a callback function.
JavaScript's callback function trivia