Little Overlord's Fun Function Callback

The Xiaobawang game console is a previous generation of popular home game consoles in China, which became popular in the early 1990s. At that time, due to the limited game software, the domestic game market was relatively scarce, which made the Xiaobawang game console an unforgettable part of the childhood of many people born in the 70s and 80s.

picture

The Xiaobawang game console is divided into two main parts: the game console itself and the cartridge. The game console is a platform for running and controlling, and the cassette can be switched, allowing players to try various games. If the host and the game are bundled together, it will be a very bad thing for the players.

Programming is a process of gathering the strengths of many families and drawing on various excellent ideas. When we design a timer function to perform certain operations after reaching a specified time, the best practice is to avoid hard-coding the specific operations in the function. This is like combining a game console and a cassette, resulting in a function that can only do one thing, which is obviously not ideal. What should a good design look like? The timer is responsible for timing, and it is up to the caller to decide what to do. Doesn't this achieve the separation of game consoles and cassettes? The timer is only responsible for timing. When the time is up, you tell me what to do, and I will do what. It is simple and efficient.

Fortunately, JavaScript has built-in timer functions for us. One of the delay timers, setTimeout, has two parameters. The first parameter is the parameter of the function type, and the second parameter is the number of milliseconds after the execution of the first one. function of parameters. Let's have a small test. After 1 second, the console outputs "Qianfeng Digital Wisdom Front-End", the code is as follows:

function printQfedu(){
   
     console.log("千锋数字智慧大前端");}setTimeout(printQfedu, 1000);

This code will output "Qianfeng Digital Wisdom Frontend" to the console 1 second after execution. It should be noted that printQfedu can be used as a variable, which represents the entire function. From this code, we can see that we have passed a task to the timer, and the timer will execute the incoming task after 1 second, that is to say, in the place where we cannot see the code, " printQfedu()" function.

So what does this have to do with callbacks? Yes, this callback can be used in two non-material and non-standard terms. The first is to go back and execute the passed function (like whether to treat you to dinner another day), and the other is to call back, back and forth, then

What is a callback function

A callback function is a common concept in programming, which refers to passing a function as a parameter to another function and executing it when a specific event or condition occurs. In asynchronous programming, callback functions are often used to handle the result of an asynchronous operation or the occurrence of an event. The use of the callback function enables the program to execute specific logic at the right time, improving the flexibility and scalability of the code

The benefits of callback functions

Simply put, the main benefit of callback functions is to implement non-blocking operations in asynchronous programming, allowing code to execute corresponding logic after an event completes without having to wait for the event to complete. This can improve the responsiveness and efficiency of the program, making the code more flexible and extensible

Common callback function usage

    1. Timer: Use the setTimeout or setInterval function to set the timer, and execute the callback function when the specified time arrives. (When the specified time is reached, call the function we wrote ourselves)
    2. Asynchronous request: When making network requests or server-side operations, callback functions are usually used to process the results of asynchronous operations. When the request is completed, the callback function will be called to process the data or error returned by the server. (Set a result, how to deal with it)
    3. Promise: When using Promise for asynchronous programming, callback functions will be used extensively. (The constructor is a callback function. When data is obtained in different channels, then parameters or catch parameters)
    4. Define custom functions: In custom functions, you can receive a callback function as a parameter, so that the behavior of the function can be determined by caller to define.
    5. Among the common methods of arrays, map, reduce, every, etc., the parameters are all callback functions.

Summarize

The callback function can be regarded as an incoming task, which triggers execution when a specific condition is met (such as a timer), or judges and processes according to this task (such as an array method)

 

Guess you like

Origin blog.csdn.net/sdasadasds/article/details/132161384