Home  >  Article  >  Web Front-end  >  How to use callback method in jquery

How to use callback method in jquery

WBOY
WBOYOriginal
2022-05-18 17:03:182150browse

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);”.

How to use callback method in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to use callback method in jquery

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:

How to use callback method in jquery

##After clicking the button:

How to use callback method in jquery

How to use callback method in jquery

Recommended related video tutorials:

jQuery video tutorial

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!

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