setInterval与clearInterval的使用示例代码
setInterval是一个很有用的js函数,可以用来重复执行某些功能,利用这个我们可以实现一些很有趣的功能,比如:
不刷新页面的情况下,"实时"获取其它会员给你发来的问候,并弹出显示之类
下面给一个示例代码:(里面用了一些jquery的方法)
<html>
<head>
<title>jquery 操作 Select</title>
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<script type="text/javascript">
var i=1;
var _interval;
function showTime()
{
var today = new Date();
$("#msg").html(today.toLocaleString() + ",i=" + i);
i++;
if (i>10)
{
clearInterval(_interval);
}
}
$(document).ready(function(){
$("#btnStart").click(function(){
showTime();
_interval = setInterval("showTime()", 1000);
})
$("#btnStop").click(function(){
clearInterval(_interval);
i=0;
})
})
</script>
</head>
<body>
<label id="msg"></label>
<button id="btnStart">开始记时</button>
<button id="btnStop">停止记时</button>
<script type="text/javascript"></script>
</body>
</html>
相关文章
全面解析jQuery中的$(window)与$(document)的用法区别
这篇文章主要介绍了jQuery中的$(window)与$(document)的用法区别,具体内容大家可查看下文的详细讲解,感兴趣的小伙伴们可以参考一下。2017-08-08
jQuery删除节点的三个方法即remove()detach()和empty()
jQuery提供了三种删除节点的方法,即remove(),detach()和empty(),下面为大家详细介绍下jQuery删除节点三个方法的具体使用2013-12-12


最新评论