Javascript倒计时
JS脚本是在网上找到的,然后进行了改进,弥补了不足之处。
JS脚本:
1 <script language="javascript" type="text/javascript">
2 var interval = 1000;
3 function ShowCountDown(year, month, day, divname) {
4 var now = new Date();
5 var endDate = new Date(year, month - 1, day, 14, 07);
6 var leftTime = endDate.getTime() - now.getTime();
7 var leftsecond = parseInt(leftTime / 1000);
8 var day1 = Math.floor(leftsecond / (60 * 60 * 24));
9 var hour1 = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
10 var hour = Math.floor((leftsecond - 60 * 60) / 3600);
11 //如果小时为负数 显示0
12 if (hour > 0) {
13 }
14 else {
15 hour = 0;
16 }
17 if (day1 < 0) {
18 hour = hour1
19 }
20 var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600) / 60);
21 var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600 - minute * 60);
22 var cc = document.getElementById(divname);
23 //如果结束时间为负数 就显示0
24 if (leftTime > 0) {
25 cc.innerHTML = hour + "小时" + minute + "分" + second + "秒";
26 //如果显示年份 取消注释
27 //cc.innerHTML = "距离" + year + "年" + month + "月" + day + "日还有:" + hour + "小时" + minute + "分" + second + "秒";
28 }
29 else {
30 cc.innerHTML = 0 + "小时" + 0 + "分" + 0 + "秒";
31 }
32 }
33 //showData是DIV的ID 2010,8,10表示是年月日的意思 参数根据自己的需求设置。
34 window.setInterval(function () { ShowCountDown(2010, 8, 10, 'showData'); }, interval);
35 </script>
2 var interval = 1000;
3 function ShowCountDown(year, month, day, divname) {
4 var now = new Date();
5 var endDate = new Date(year, month - 1, day, 14, 07);
6 var leftTime = endDate.getTime() - now.getTime();
7 var leftsecond = parseInt(leftTime / 1000);
8 var day1 = Math.floor(leftsecond / (60 * 60 * 24));
9 var hour1 = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
10 var hour = Math.floor((leftsecond - 60 * 60) / 3600);
11 //如果小时为负数 显示0
12 if (hour > 0) {
13 }
14 else {
15 hour = 0;
16 }
17 if (day1 < 0) {
18 hour = hour1
19 }
20 var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600) / 60);
21 var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600 - minute * 60);
22 var cc = document.getElementById(divname);
23 //如果结束时间为负数 就显示0
24 if (leftTime > 0) {
25 cc.innerHTML = hour + "小时" + minute + "分" + second + "秒";
26 //如果显示年份 取消注释
27 //cc.innerHTML = "距离" + year + "年" + month + "月" + day + "日还有:" + hour + "小时" + minute + "分" + second + "秒";
28 }
29 else {
30 cc.innerHTML = 0 + "小时" + 0 + "分" + 0 + "秒";
31 }
32 }
33 //showData是DIV的ID 2010,8,10表示是年月日的意思 参数根据自己的需求设置。
34 window.setInterval(function () { ShowCountDown(2010, 8, 10, 'showData'); }, interval);
35 </script>
HTML:
1 <div id="showData">
2 </div>
2 </div>
作者:Mr S.R Lee
出处:http://www.cnblogs.com/LeeYongze
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.