setInterval和setTimeout的不同点
setInterval
是在规定的时间段无限的执行对应的事件,停止它的语句window.clearInterval(time)
setTimeout
是在规定的时间段只执行一次
setInterval和setTimeout的相同点就是语法相同
setInterval(事件,时间)
setTimeout
(事件,时间)
下面是我没事的时候jquery写的一个小例子,复制下来就能用
<html>
<head>
<title></title>
<script src="js/jquery-1.6.2.min.js"
type="text/javascript"></script>
<script type="text/javascript">
var count = 0;
var checktime;
var time;
$(function () {
$("#btn").click(function () {
checktime = setInterval(check, 500);
time = setInterval(begin, 1000);
});
$("#btn1").click(function () {
checktime = setTimeout(ggg, 5000);
});
})
function begin() {
$("#btn").text(5-count);
count++;
}
function check() {
if(count>5)
{
window.clearInterval(time);
//结束setInterval循环
window.clearInterval(checktime);
alert("ok");
}
}
function ggg() {
alert("88888888888888");
}
</script>
</head>
<body>
<div>
<h2
id="btn">setInterval单击我看看</h2>
<h2
id="btn1">setTimeout单击我看看</h2>
</div>
</body>
</html>