Javascript (js) 倒计时显示当年剩下的天时分秒
一道JS大面试题
题目在网页里倒计时显示当年剩下的天时分秒,格式:XX年还剩下X天X时X分X秒
(function() {
var day = hour = minitus = second = ms = 0;
var year = new Date().getFullYear() + 1;
var tmp = new Date(year, 0, 1) - new Date(); day = Math.floor(tmp / 86400000);
(function() {
tmp = (new Date(year, 0, 1) - new Date()) % 86400000;
hour = Math.floor(tmp / 3600000);
tmp %= 3600000;
minitus = Math.floor(tmp / 60000);
tmp %= 60000;
second = Math.floor(tmp / 1000);
ms = tmp - 1000 * second;
document.getElementById("m1").innerHTML = "距离" + year + "年还有 " + day + " 天 " + hour + " 小时 " + minitus + " 分 " + second + " 秒 " + ms;
setTimeout(arguments.callee, 50);
})();
})();
var day = hour = minitus = second = ms = 0;
var year = new Date().getFullYear() + 1;
var tmp = new Date(year, 0, 1) - new Date(); day = Math.floor(tmp / 86400000);
(function() {
tmp = (new Date(year, 0, 1) - new Date()) % 86400000;
hour = Math.floor(tmp / 3600000);
tmp %= 3600000;
minitus = Math.floor(tmp / 60000);
tmp %= 60000;
second = Math.floor(tmp / 1000);
ms = tmp - 1000 * second;
document.getElementById("m1").innerHTML = "距离" + year + "年还有 " + day + " 天 " + hour + " 小时 " + minitus + " 分 " + second + " 秒 " + ms;
setTimeout(arguments.callee, 50);
})();
})();
为了优化代码,day只计算了一次。hour,minitus,second,ms则放在里层反复计算
一道JS大面试题
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>一道JS面试题</title>
<style>
body{
font-size:12px;
font-family:"微软雅黑";
line-height:1.8;
}
</style>
</head>
<body>
<h3>一道JS大面试题</h3>
<p>题目在网页里倒计时显示当年剩下的天时分秒,格式:XX年还剩下X天X时X分X秒</p>
<div id="m1"></div>
<script type="text/javascript">
(function(){
var day=hour=minitus=second=ms=0;
var year=new Date().getFullYear()+1;
var tmp=new Date(year,0,1)-new Date();
day=Math.floor(tmp/86400000);
(function(){
tmp=(new Date(year,0,1)-new Date())%86400000;
hour=Math.floor(tmp/3600000);
tmp%=3600000;
minitus=Math.floor(tmp/60000);
tmp%=60000;
second=Math.floor(tmp/1000);
ms=tmp-1000*second;
document.getElementById("m1").innerHTML="距离"+year+"年还有 "+day+" 天 "+hour+" 小时 "+minitus+" 分 "+second+" 秒 "+ms;
setTimeout(arguments.callee,50);
})();
})();
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>一道JS面试题</title>
<style>
body{
font-size:12px;
font-family:"微软雅黑";
line-height:1.8;
}
</style>
</head>
<body>
<h3>一道JS大面试题</h3>
<p>题目在网页里倒计时显示当年剩下的天时分秒,格式:XX年还剩下X天X时X分X秒</p>
<div id="m1"></div>
<script type="text/javascript">
(function(){
var day=hour=minitus=second=ms=0;
var year=new Date().getFullYear()+1;
var tmp=new Date(year,0,1)-new Date();
day=Math.floor(tmp/86400000);
(function(){
tmp=(new Date(year,0,1)-new Date())%86400000;
hour=Math.floor(tmp/3600000);
tmp%=3600000;
minitus=Math.floor(tmp/60000);
tmp%=60000;
second=Math.floor(tmp/1000);
ms=tmp-1000*second;
document.getElementById("m1").innerHTML="距离"+year+"年还有 "+day+" 天 "+hour+" 小时 "+minitus+" 分 "+second+" 秒 "+ms;
setTimeout(arguments.callee,50);
})();
})();
</script>
</body>
</html>