Home  >  Article  >  Web Front-end  >  About the practical application of jQuery Callback function

About the practical application of jQuery Callback function

jacklove
jackloveOriginal
2018-05-04 15:18:071297browse

We often see the callback function in our studies. This article will explain in detail how to use the callback function Callback.

jQuery Problems with animation

Many jQuery functions involve animation. These functions may take speed or duration as optional parameters.

Example:

$("p").hide("slow")

The speed or duration parameter can be set to many different values, such as "slow", "fast", "normal" or milliseconds.

Example

$("button").click(function(){
$("p").hide(1000);
});

Since JavaScript statements (instructions) are executed one by one - in order, the statements after the animation may generate errors or page conflicts because the animation has not yet Finish.

To avoid this situation, you can add the Callback function in the form of parameters.

jQuery Callback function

When the animation is 100% complete, the Callback function is called.

Typical syntax:

$(selector).hide(speed,callback)

The callback parameter is a function that is executed after the hide operation is completed.

Error (no callback)

$("p").hide(1000);
alert("The paragraph is now hidden");

Correct (with callback)

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

This article explains some usage of the callback function callback. I want to learn more about this. For knowledge, please pay attention to php Chinese website.

Related recommendations:

Detailed explanation of javscript's callback function

Usage of php regular preg_replace_callback function

php Callback Detailed explanation of callback function

The above is the detailed content of About the practical application of jQuery Callback function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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