定时器设置与取消

写在前面

通过JavaScript原生的setInterval()和clearInterval()设置一个定时器和取消该定时器,从而控制页面自动生成一些内容并提供停止的功能。

代码

<p id="para">para:p>
<button>stopbutton>
<script>function add() {$("#para").append("add this is strong something
"
);}var repeat=setInterval("add()",1500);$("button").click(function () {clearInterval(repeat);});
script>

add函数向页面的一段增加内容;
var repeat=setInterval("add()",1500);设置定时器,定时间隔为1500毫秒。
页面中的button提供停止定时器的功能。通过clearInterval()实现,而它的参数为定义的定时器。

代码全文


<html lang="en">
<head><meta charset="UTF-8"><title>Titletitle><script src="../../lib/jquery/jquery.js">script>
head>
<body>
<p id="para">para:p>
<button>stopbutton>
<script>function add() {$("#para").append("add this is strong something
"
);}var repeat=setInterval("add()",1500);$("button").click(function () {clearInterval(repeat);});
script> body> html>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部