首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

改为一个十天的倒计时

2012-08-14 
改成一个十天的倒计时求JS大牛把下面的改成一个十天的倒计时,十天之后,这个DIV就消失!DOCTYPE HTMLhtml

改成一个十天的倒计时
求JS大牛把下面的改成一个十天的倒计时,十天之后,这个DIV就消失



<!DOCTYPE HTML>
<html>
 <head>
  <title> New Document </title>
  <meta charset="gb2312">
  <style>
#xs{margin:20px 0 0 0;color:#000;width:400px;margin:0 auto;font-weight:bolder;letter-spacing:5px}
#xs b{color:red;}
  </style>
 </head>
 <body onload="nowTime()">
  <script>
  function nowTime(){
  var td=new Date()
  var h=23-td.getHours()
  if (h<10)h="0"+h;
  var m=59-td.getMinutes()
  if (m<10)m="0"+m;
  var s=60-td.getSeconds()
  if (s<10)
  {s="0"+s
  }else if (s==60)
  {
s="00"
  }
  document.getElementById('xs').innerHTML="<b>距明天还有:</b>"+h+"<b>小时</b>"+m+"<b>分</b>"+s+"<b>秒</b>"
  t=setTimeout('nowTime()',50)
  }
  </script>
  <div id='xs' >
  </div>
 </body>
</html>


[解决办法]
用下面这个试试
设置定时var target = new Date(2012, 06, 9, 00, 00, 00).getTime();06表示7月 
<html> 
<head> 
<title>Javascript写的倒计时效果代码</title> 
</head> 
<body> 
<script language="javascript" type="text/javascript"> 
var target = new Date(2012, 06, 9, 00, 00, 00).getTime(); 
function show_date_time(){ 
setTimeout("show_date_time()", 1000); 
today=new Date();
timeold = target - today.getTime(); 
sectimeold=timeold/1000; 
secondsold=Math.floor(sectimeold); 
msPerDay=24*60*60*1000; 
e_daysold=timeold/msPerDay; 
daysold=Math.floor(e_daysold); 
e_hrsold=(e_daysold-daysold)*24; 
hrsold=Math.floor(e_hrsold); 
e_minsold=(e_hrsold-hrsold)*60; 
minsold=Math.floor((e_hrsold-hrsold)*60); 
seconds=Math.floor((e_minsold-minsold)*60);
if (daysold < 0) {
document.body.removeChild(document.getElementById("TimeCounter_0"));
return null;
}
else { 
if (daysold<10) {daysold="0"+daysold} 
if (hrsold<10) {hrsold="0"+hrsold} 
if (minsold<10) {minsold="0"+minsold} 
if (seconds<10) {seconds="0"+seconds} 
if (daysold<3) {
document.getElementById("TimeCounter_0").innerHTML = "<font color=red>" + daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒</font>"; 
}
else {
document.getElementById("TimeCounter_0").innerHTML = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒"; 



window.onload=show_date_time;
</script> 
距10天后还有: 
<div id="TimeCounter_0" style="border:1px solid black;margin:5px;padding:2px;width:140px"></div> 
</body> 
</html>
[解决办法]
倒计时总是需要一个参考时间的,要么是开始时间、要么是结束时间。

HTML code
<!DOCTYPE HTML><html><head><title>New Document</title><meta charset="utf-8"><style>#xs {    margin:20px 0 0 0;    color:#000;    width:400px;    margin:0 auto;    font-weight:bolder;    letter-spacing:5px}#xs b {    color:red;}</style><script type="text/javascript">var sEXP = '2012-07-09 23:59:59'; //设置到期时间var oEXP = new Date(sEXP); //到期时间转换为Date对象var t;function countdown(oEXP) {    var now = new Date();    var xs = document.getElementById('xs');    if (now > oEXP) {        xs.parentNode.removeChild(xs); //到期移除div元素        window.clearInterval(t);    }    else {        var oTimeDiff = {};        var iTimeDiff = Math.round((oEXP.getTime() - now.getTime()) / 1000);        oTimeDiff.h = Math.floor(iTimeDiff / 3600), iTimeDiff -= oTimeDiff.h * 3600;        oTimeDiff.m = Math.floor(iTimeDiff / 60);        oTimeDiff.s = iTimeDiff - oTimeDiff.m * 60;        xs.innerHTML = '<b>距离' + sEXP + '还有:' + oTimeDiff.h + '小时' + oTimeDiff.m + '分' + oTimeDiff.s + '秒</b>';    }}window.onload = function() {    t = window.setInterval('countdown(oEXP)', 1000);}</script></head><body><div id='xs'></div></body></html> 

热点排行