Home > Article > Web Front-end > How to use callback method in jquery
In jquery, the callback method is used to pass as a parameter of the effect method. This method is executed after the current action is completely completed. It is usually displayed as the last parameter of the method. The syntax is "$(selector).hide (duration, easing, callback);”.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
JavaScript statement to execute line by line. However, since jQuery effects take some time to complete, the next line of code may be executed while the previous effect is still running. This will generate errors.
To prevent this from happening, jQuery provides a callback function for each effect method.
After the current effect is completed, the callback function will be executed.
Callback functions are passed as arguments to effect methods, and they usually appear as the last argument to the method.
Typical syntax: $(selector).hide(duration, easing, callback);
The following example has a callback parameter, which is a function that will be executed after the hiding effect is completed:
The example is as follows:
<!DOCTYPE html> <html> <title>jQuery 使用回调函数示例</title> <head> <script src="js/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide("slow", function(){ alert("段落现在隐藏起来了"); }); }); }); </script> </head> <body> <button>隐藏</button> <p>这是一个段落。</p> </body> </html>
Output result:
##After clicking the button:Recommended related video tutorials:
The above is the detailed content of How to use callback method in jquery. For more information, please follow other related articles on the PHP Chinese website!