Callback function concept: A callback function is a function called through a function pointer. If you pass a function pointer (address) as a parameter to another function, and when this pointer is used to call the function it points to, we say it is a callback function.
JS Api explains this: A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.
The reason for using callback function: you can The caller is separated from the callee. The caller doesn't care who the callee is, all it needs to know is that there is a called function with a certain prototype and certain restrictions (such as a return value of int).
Consider an example like this:
Suppose the bottom and top levels of a project are completed by different people. The bottom level is responsible for data access, and the top level is responsible for data presentation. When the top level wants to The data of a certain module was used, so he said to the bottom-level staff, I need you to provide data that meets certain needs, and you provide me with an interface.
The bottom-level staff said: I will provide you with the data. How to display and process it is your business. It is impossible for me to provide a data interface for every need. I will provide you with a passable interface. You get the data and then write your own function to display it. After negotiation, both parties Provides such an interface:
Callback function example: when When the student score<=0, it will be processed by the bottom layer; when the score>0, it will be processed by the upper layer.