<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<head>
<title> 演示实例:倒计时效果-精确到秒 </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="author" content="枫岩,CNLei.y.l@gmail.com">
<meta name="keywords" content="" />
<meta name="description" content="" />
<script language="JavaScript">
<!-- //
function GetRTime(){
var EndTime= new Date(2008,10,13,10,0); //截止时间:2009年6月10日0时0分
var NowTime = new Date();
var nMS =EndTime.getTime() - NowTime.getTime();
var nD =Math.floor(nMS/(1000 * 60 * 60 * 24));
var nH=Math.floor(nMS/(1000*60*60)) % 24;
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
if(nD>= 0){
document.getElementById("RemainD").innerHTML=nD;
document.getElementById("RemainH").innerHTML=nH;
document.getElementById("RemainM").innerHTML=nM;
document.getElementById("RemainS").innerHTML=nS;
}
else {
document.getElementById("CountMsg").innerHTML="世界杯已经圆满结束!";
}
setTimeout("GetRTime()",1000);
}
window.onload=GetRTime;
//setInterval("GetRTime()",1000);
// -->
</script>
</head>
<body>
<div id="CountMsg">今天距离2006世界杯开赛还有<strong id="RemainD">XX</strong>天<strong id="RemainH">XX</strong>时<strong id="RemainM">XX</strong>分<strong
id="RemainS">XX</strong>秒</div>
</body>
</html>
var timer1 = setTimeout("foo",1000); //设定foo函数将在1000毫秒后执行一次
.........//do something
clearTimeout(timer1); //如果程序执行到这里,以setTimeout开始计数时1000毫秒未到,
//clearTimeout后,foo将得不到执行
//如果1000毫秒已过,foo已被触发执行,就没什么意义了
vartimer2 = setInterval("foo",1000); //设定从现在计时起,每1000毫秒执行1次foo
.........//do something
clearInterval(timer2); //以后foo将不再触发执行了
所以这样一个细节的被忽略了,也就长久以来用setTimeout+递归来模拟了setInterval的行为,如下代码
function foo(){
.......//do something
setTimeout("foo",1000);