Home > Web Front-end > JS Tutorial > body text

A brief analysis of callback function examples in JS

亚连
Release: 2018-05-26 17:05:40
Original
1245 people have browsed it

This article mainly introduces the callback function in JS, and briefly analyzes the concept, function, usage and related precautions of the javascript callback function in the form of examples. Friends in need can refer to the examples of this article

Describes the callback function in JS. Share it with everyone for your reference, the details are as follows:

Before talking about the callback function, you might as well look at a piece of code first. I believe that students with some basic js can understand its meaning:

document.getElementById('demo').click=function(){
  alert(1);
};
Copy after login

This code is actually an event callback. It is actually relatively vague when written this way. We might as well look at the next code

document.getElementById('demo').addEventListener('click',function(){
    alert(1)
});
Copy after login

The two pieces of code actually do the same thing. The only difference is the way of writing. Let’s look at thisaddEventListener('eventName',callback),addEventListenerthis The function has two parameters. The first one is the event name, and the second parameter is actually the callback function. According to the callback function method in the book, since the parameters in the function can be variables, then it can also be a function. Maybe everyone is still confused about returning functions at this point. Let's look at the next example.

function demo(a,b,callback){
    let c=a+b;
    callback(c);
};
demo(1,2,function(c){
    alert(c);//3
})
Copy after login

This code defines a demo function. This function has three parameters a, b, callback. We declare a local variable c inside this function. , and then execute our callback (callback function), and then execute the demo function

The three parameters of this function are as above, and the value that pops up in the callback function is 3. This is a simple callback function. To truly understand the meaning of the callback function, I actually think it depends on understanding its purpose. Only by understanding its purpose can we truly understand it.

The most common application scenario for callback functions is asynchronous operations. Because for asynchronous operations, we don’t know when the operation will end, then the code we execute subsequently cannot follow the proper process.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Loop scheme in Ajax

##A simple example of using XPath in dom4j

A brief discussion on Bootstrap's DatePicker date range selection

The above is the detailed content of A brief analysis of callback function examples in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template