• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

jquery怎么实现延迟执行?

jquery 搞代码 4年前 (2021-12-27) 82次浏览 已收录 0个评论

jquery怎么实现延迟执行?下面本篇文章就来给大家介绍一下jquery实现延迟执行的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

方法1:使用delay()

delay() 方法对队列中的下一项的执行设置延迟。

示例:

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
		<style>
			#div {
				background: lightcoral;
				height: 100px;
				width: 200px;
				line-height: 100px;
				margin: 0 auto;
				color: white;
			}
		</style>

	</head>

	<body style="text-align:center;">
		<p style="font-size: 19px; font-weight: bold;">
			单击按钮,在2秒后隐藏DIV元素。
		</p>
		<div id="div"> 一个DIV盒子 </div>
		<br>
		<button onClick="Fun()">点击这里</button>
		<p id="DOWN" style="color: lightcoral; font-size: 24px; font-weight: bold;"></p>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
		<script>
			function Fun() {
				$("#div").delay(2000).fadeOut('fast');
				$("#DOWN").text("DIV元素在2秒后隐藏");
			}
		</script>
	</body>
</html>

方法2:使用setTimeOut()

setTimeOut()用于从载入后延迟指定的时间去执行一个表达式或者是函数。

提示: 1000 毫秒= 1 秒。

提示: 如果你只想重复执行可以使用 setInterval() 方法。

提示: 使用 clearTimeout() 方法来阻止函数的执行。

示例:

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
		<style>
			#div {
				background: lightcoral;
				height: 100px;
				width: 200px;
				line-height: 100px;
				margin: 0 auto;
				color: white;
			}
		</style>

	</head>

	<body style="text-align:center;">
		<p style="font-size: 19px; font-weight: bold;">
			单击按钮,在1秒后隐藏DIV元素。
		</p>
		<div id="div"> 一个DIV盒子 </div>
		<br>
		<button onClick="Fun()">点击这<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>里</button>
		<p id="DOWN" style="color: lightcoral; font-size: 24px; font-weight: bold;"></p>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
		<script>
			function Fun() {
				setTimeout(function() {
					$("#div").fadeOut('fast');
				}, 1000);
				$("#DOWN").text("DIV元素在1秒后隐藏");
			}
		</script>
	</body>
</html>

更多web开发知识,请查阅 搞代码网 !!

以上就是jquery怎么实现延迟执行?的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:jquery怎么实现延迟执行?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址