Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use callback function in js

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use the callback function in js". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use callback function in js".

Detailed explanation of JS callback function

JS callback function

What is a callback function, the official explanation: when the program is running, in general, the application (application program) will often call the pre-prepared functions in the library through API. However, some library functions (library function) require the application to pass it a function first so that it can be called at the appropriate time to complete the target task. The function that is passed in and then called is called the callback function function.

Usually pass one function B to another function A, and then call function A when needed.

To put it bluntly, a callback is a backtracking, which defines the function body to be used, and then calls this function after using make. We usually pass callback as an argument to the previously defined function. Let's first look at a piece of jquery code:

$("p") .hide (1000 dint function () {alert ("The paragraph is now hidden");})

The above jquery is a callback function, which first calls the function callback function after executing the hide effect.

Let's look at another piece of JS code and see how the callback function is implemented:

Function Buy (name,goods1,callback) {alert (name+' buy'+ goods1); if (callback&&typeof (callback) = "function") callback ();} Buy ('xiaoming','apple',function () {alert ("shopping finish");})

A very simple code that does not know what to buy at first, but immediately calls the previously defined function when it is bought. It is best to add judgment rules, because all the premise is that callback must be a function, and the output is as follows:

Xiaoming buy appleshopping finish

Closure and callback

Let's take a look at the topic of a closure. Click on the first and fourth nodes, respectively, and execute the results:

Var nodes = document.getElementsByTagName ('button'); for (var I = 0; I < nodes.length; iTunes +) {click', function [I] .addEventListener (' click', function () {Nodes ('You clicked element #' + I);});}

Although the main test here is the closure, addEventListener is a closure, and the anonymous function is a callback function, and I is a variable in the closure. When the callback of addEventListener is executed, the loop has ended, and the variable I is assigned to node.length,node.length as the total number of nodes. So the result is

You clicked element # node.length

So how to change it? change its return value to a function. Since the value of the I variable will be released, let's introduce I so that the value of each loop is stored in memory.

Var nodes = document.getElementsByTagName ('button'); for (var I = 0; I < nodes.length; iTunes +) {(function (I) {Nodes [I] .addEventListener (' click', function () {console.log ('Nodes #' + I);}) (I);} PHP callback function

Here is a brief description of how to implement the callback function in php (the result is 1, 2, 3, 4):

Php callbacks each passed array element. Let's take a look at the closure of php, using the use keyword to inherit variables outside the scope:

The returned result is:

1,1

Official php usage

Class hello {function callback ($a _ hello','callback') {echo "$a _ r _ r _ b";} static function callback ($c _ force _ quod) {echo "$c _ Eng _ r";}} / take the class name as the parameter call_user_func (array ('hello','callback'), "hello", "world"); / / output the hello,world// takes the object as the parameter call_user_func (array (new hello (),' callback'), "hello", "world"). / / take the static method as the parameter call_user_func (hello::callback, "hello", "world"); / / output hello,world

In fact, as far as the principle is concerned, JS and php are similar, defining functions first, and then calling them when needed.

These are all the contents of this article entitled "how to use callback functions in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report