如何在一定延迟后调用 jQuery 函数?
jqueryweb developmentfront end technology
要在一定延迟后调用 jQuery 函数,请使用 siteTimeout() 方法。此处,jQuery fadeOut() 函数会在几秒后调用。
您可以尝试运行以下代码,了解如何使用 jQuery 中的 setTimeout() 方法在延迟后调用 jQuery 函数。 −
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").bind("click",function() {
setTimeout(function() {
$('#list').fadeOut();}, 2000);
});
});
</script>
</head>
<body>
<input type="button" id="button1" value="Fade Out" />
<br/>
<br/>
<div id="list">
<ul>
<li>Voice</li>
<li>Speak</li>
<li>Write</li>
</ul>
</div>
<p>The above content will fade out after 2 seconds</p>
</body>
</html>

