延迟代码执行window.setTimeout(code,delay);
eg:
<script>
var ident;
ident = window.setTimeout("alert('延时时间到了!!!')",1000);
</script>
周期性执行代码
setInterval(code,1000);--------------------每分钟调用code一次
eg:
<script>
function myFun()
{
setInterval("setsDate()",1000);
}
function setsDate()
{
var myDate = new Date();
myForm.showDate.value = myDate.toLocaleString();
}
</script>
</head>
<body onload="myFun()">
<form name="myForm">
当前时间为:<input type="text" name="showDate" size="25" />
</form>
</body>
停止周期性执行代码
使用setInterval方法可以设定计时器,设定计时器时将返回一个计时器的引用,当不再需要的时候可以使用clearInterval方法移除计时器,其接收一个计时器ID作为参数
window.clearInterval(id)
eg:
<script>
var tm = 0;
var count = 0
function ReloadPage()
{
if(window.confirm("是否要重新加载?"))
{
window.location.reload();
}
else
{
if(++count==3)
{
ClearInterval(tm);
}
}
}
tm = setInterval("ReloadPage()",1000);
</script>