---
title: "js 倒计时"
author: "wfdoublext"
date: "2008-11-13T09:28:33Z"
tags: ["Gmail", "HTML", "JavaScript", "XHTML"]
slug: "js"
draft: false
lastmod: "2008-11-13T09:39:08Z"
id: 266430
url: "https://skyofdawn.iteye.com/blog/266430"
---

&lt;!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt;html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)" lang="gb2312"&gt;  
&lt;head&gt;  
&lt;head&gt;  
&lt;title&gt; 演示实例：倒计时效果-精确到秒 &lt;/title&gt;  
&lt;meta http-equiv="content-type" content="text/html; charset=gb2312" /&gt;  
&lt;meta name="author" content="枫岩,CNLei.y.l@gmail.com"&gt;  
&lt;meta name="keywords" content="" /&gt;  
&lt;meta name="description" content="" /&gt;  
&lt;script language="JavaScript"&gt;  
&lt;!-- //  
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&gt;= 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);  
// --&gt;  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div id="CountMsg"&gt;今天距离2006世界杯开赛还有&lt;strong id="RemainD"&gt;XX&lt;/strong&gt;天&lt;strong id="RemainH"&gt;XX&lt;/strong&gt;时&lt;strong id="RemainM"&gt;XX&lt;/strong&gt;分&lt;strong

id="RemainS"&gt;XX&lt;/strong&gt;秒&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;

 

 

 

 

 

**var** timer1 = setTimeout("foo",1000);  //设定foo**函数**将在1000毫秒后执行一次  
.........//do something

clearTimeout(timer1); //如果程序执行到这里,以setTimeout开始计数时1000毫秒未到,  
                                //clearTimeout后，foo将得不到执行  
                                //如果1000毫秒已过，foo已被触发执行，就没什么意义了

**var**timer2 = **setInterval**("foo",1000); //设定从现在计时起，每1000毫秒执行1次foo  
.........//do something

clearInterval(timer2);  //以后foo将不再触发执行了

所以这样一个细节的被忽略了，也就长久以来用setTimeout+递归来模拟了**setInterval**的行为，如下代码  
**function** foo(){  
    .......//do something  
    setTimeout("foo",1000);