Home > Web Front-end > JS Tutorial > JS callback function uses parameters

JS callback function uses parameters

php中世界最好的语言
Release: 2018-06-14 14:23:54
Original
1698 people have browsed it

This time I will bring you the parameters used by the JS callback function. What are the precautions for using the parameters of the 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.

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.

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:

p5.js uses the draw() function to make a golden spiral

How to keep-alive with vue Operating website cache

The above is the detailed content of JS callback function uses parameters. 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