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

Case description of using JS callback function

php中世界最好的语言
Release: 2018-05-09 10:28:46
Original
1378 people have browsed it

This time I will bring you a case description of using JScallback function, what are the notes of using JS callback function, the following is a practical case, let's take a look.

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

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

This code is actually an event callback. Write it like this In fact, it is relatively vague. Let's take a look at the following 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 function has two parameters, the first is the event name, and the second parameter is actually the callback function. According to the callback function in the book, the function Since the parameter in can be a variable, 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), then Come down and execute the demo function

The three parameters of this function are as above, and the value 3 pops up in the callback function. 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to use WebUploader in the fuzzy box

Detailed explanation of computed use cases in Vue.js

The above is the detailed content of Case description of using JS callback function. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!