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搞代码网其它相关文章!