关于客户端javascript倒计时误差的问题 我使用一个.html页面,里面有一段javascript倒计时程序,使用setInterval,定时调用的函数里有一段不太长的代码,应该在一秒钟内可以结束。不知道时间长了,这个倒计时的误差会不会挺大的? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 会,经测试用setInterval几分钟就能看到秒级别的误差。 你可以利用客户端的时间来做纠正在ie下,setinterval比较准,但是在ff和chrome下面就会快一点,如果你的倒计时精度不是很高,比如毫秒级。你可以利用客户端的时间来处理。js方面就是开始的时候获取一下客户端时间getTime作为零坐标,然后setInterval来获取新的客户端去差值来倒计时。为了提高精度,你的setInterval的间隔可以设置小一点,如果你设置1秒就没有任何价值了,建议50ms? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN<html ><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>JavaScript ʱʾ</title></head><body><span id=localtime></span><span id=checktime></span><script type="text/javascript">var first=1;function showLocale(objD){var str,colorhead,colorfoot;var yy = objD.getYear();if(yy<1900) yy = yy+1900;var MM = objD.getMonth()+1;if(MM<10) MM = '0' + MM;var dd = objD.getDate();if(dd<10) dd = '0' + dd;var hh = objD.getHours();if(hh<10) hh = '0' + hh;var mm = objD.getMinutes();if(mm<10) mm = '0' + mm;var ss = objD.getSeconds();if(ss<10) ss = '0' + ss;var ww = objD.getDay();if ( ww==0 ) colorhead="<font color=\"#FF0000\">";if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";if ( ww==6 ) colorhead="<font color=\"#008000\">";if (ww==0) ww="";if (ww==1) ww="һ";if (ww==2) ww="ڶ";if (ww==3) ww="";if (ww==4) ww="";if (ww==5) ww="";if (ww==6) ww="";colorfoot="</font>"str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot;return(str);}var str,colorhead,colorfoot;var yy;var MM;var dd;var hh;var mm;var ss;var ww;var checktoday;checktoday = new Date();function checkTime(){ var strabc="abc"; for (var i=0; i<1000; i++) { strabc+="mm"; } if (first==1) { first=0; yy = checktoday.getYear(); if(yy<1900) yy = yy+1900; MM = checktoday.getMonth()+1; if(MM<10) MM = '0' + MM; dd = checktoday.getDate(); if(dd<10) dd = '0' + dd; hh = checktoday.getHours(); if(hh<10) hh = '0' + hh; mm = checktoday.getMinutes(); if(mm<10) mm = '0' + mm; ss = checktoday.getSeconds(); if(ss<10) ss = '0' + ss; ww = checktoday.getDay(); if ( ww==0 ) colorhead="<font color=\"#FF0000\">"; if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">"; if ( ww==6 ) colorhead="<font color=\"#008000\">"; if (ww==0) ww=""; if (ww==1) ww="һ"; if (ww==2) ww="ڶ"; if (ww==3) ww=""; if (ww==4) ww=""; if (ww==5) ww=""; if (ww==6) ww=""; colorfoot="</font>" str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot; } else { ss-=0; ss+=1; if(ss<10) ss = '0' + ss; if (ss>=60) { ss='00'; mm-=0; mm++; if (mm>=60) { mm='00'; hh-=0; hh++; if (hh>=60) { hh='00'; dd++; } } } ww = checktoday.getDay(); if ( ww==0 ) colorhead="<font color=\"#FF0000\">"; if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">"; if ( ww==6 ) colorhead="<font color=\"#008000\">"; if (ww==0) ww=""; if (ww==1) ww="һ"; if (ww==2) ww="ڶ"; if (ww==3) ww=""; if (ww==4) ww=""; if (ww==5) ww=""; if (ww==6) ww=""; colorfoot="</font>"; str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot; } document.getElementById("checktime").innerHTML = str;}function tick(){ var today; today = new Date(); document.getElementById("localtime").innerHTML = showLocale(today); //window.setTimeout("tick()", 1000);}setInterval("tick()", 1000);setInterval("checkTime()", 1000);</script></body></html> 使用上面的代码测试,开始,两个时间显示,差别是一秒,过了半个小时后,两个显示的时间,差别仍旧是一秒。是不是差别可以说很少呢 getElementById 获取对应值的问题? 求一个JS对Table的查询操作 Access中为什么只能插入数字和字母,而插不进文字去呢?数据表中我明明设置的是文本型的啊? 字符转日期的问题 问各位个简单的正则表达式。 求一正则表达式过滤掉空白字符但是保留单独的右斜杠。 JavaScript 的 window.history.go(1) 匹配img的src 在线等候。。请问在javascript怎样动态将窗口一分为二 jQuery缓存的对象的更新的问题 ""==0,这个表达式竟然返回 true,百思不得其解呀? jQuery删除多行的问题,解决问题之后马上结贴。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript ʱʾ</title>
</head>
<body>
<span id=localtime></span>
<span id=checktime></span>
<script type="text/javascript">
var first=1;
function showLocale(objD)
{
var str,colorhead,colorfoot;
var yy = objD.getYear();
if(yy<1900) yy = yy+1900;
var MM = objD.getMonth()+1;
if(MM<10) MM = '0' + MM;
var dd = objD.getDate();
if(dd<10) dd = '0' + dd;
var hh = objD.getHours();
if(hh<10) hh = '0' + hh;
var mm = objD.getMinutes();
if(mm<10) mm = '0' + mm;
var ss = objD.getSeconds();
if(ss<10) ss = '0' + ss;
var ww = objD.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="";
if (ww==1) ww="һ";
if (ww==2) ww="ڶ";
if (ww==3) ww="";
if (ww==4) ww="";
if (ww==5) ww="";
if (ww==6) ww="";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot;
return(str);
}var str,colorhead,colorfoot;
var yy;
var MM;
var dd;
var hh;
var mm;
var ss;
var ww;
var checktoday;
checktoday = new Date();
function checkTime()
{
var strabc="abc";
for (var i=0; i<1000; i++)
{
strabc+="mm";
}
if (first==1)
{
first=0;
yy = checktoday.getYear();
if(yy<1900) yy = yy+1900;
MM = checktoday.getMonth()+1;
if(MM<10) MM = '0' + MM;
dd = checktoday.getDate();
if(dd<10) dd = '0' + dd;
hh = checktoday.getHours();
if(hh<10) hh = '0' + hh;
mm = checktoday.getMinutes();
if(mm<10) mm = '0' + mm;
ss = checktoday.getSeconds();
if(ss<10) ss = '0' + ss;
ww = checktoday.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="";
if (ww==1) ww="һ";
if (ww==2) ww="ڶ";
if (ww==3) ww="";
if (ww==4) ww="";
if (ww==5) ww="";
if (ww==6) ww="";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot;
}
else
{
ss-=0;
ss+=1;
if(ss<10) ss = '0' + ss;
if (ss>=60)
{
ss='00';
mm-=0;
mm++;
if (mm>=60)
{
mm='00';
hh-=0;
hh++;
if (hh>=60)
{
hh='00';
dd++;
}
}
}
ww = checktoday.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="";
if (ww==1) ww="һ";
if (ww==2) ww="ڶ";
if (ww==3) ww="";
if (ww==4) ww="";
if (ww==5) ww="";
if (ww==6) ww="";
colorfoot="</font>";
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + colorfoot;
}
document.getElementById("checktime").innerHTML = str;
}
function tick()
{
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
//window.setTimeout("tick()", 1000);
}
setInterval("tick()", 1000);setInterval("checkTime()", 1000);</script>
</body>
</html>
使用上面的代码测试,
开始,两个时间显示,差别是一秒,
过了半个小时后,两个显示的时间,差别仍旧是一秒。
是不是差别可以说很少呢