JS脚本:
复制代码 代码如下:
<script language="javascript" type="text/javascript">
var interval = 1000;
function ShowCountDown(year, month, day, divname) {
var now = new Date();
var endDate = new Date(year, month - 1, day, 14, 07);
var leftTime = endDate.getTime() - now.getTime();
var leftsecond = parseInt(leftTime / 1000);
var day1 = Math.floor(leftsecond / (60 * 60 * 24));
var hour1 = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
var hour = Math.floor((leftsecond - 60 * 60) / 3600);
//如果小时为负数 显示0
if (hour > 0) {
}
else {
hour = 0;
}
if (day1 < 0) {
hour = hour1
}
var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600) / 60);
var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600 - minute * 60);
var cc = document.getElementById(divname);
//如果结束时间为负数 就显示0
if (leftTime > 0) {
cc.innerHTML = hour + "小时" + minute + "分" + second + "秒";
//如果显示年份 取消注释
//cc.innerHTML = "距离" + year + "年" + month + "月" + day + "日还有:" + hour + "小时" + minute + "分" + second + "秒";
}
else {
cc.innerHTML = 0 + "小时" + 0 + "分" + 0 + "秒";
}
}
//showData是DIV的ID 2010,8,10表示是年月日的意思 参数根据自己的需求设置。
window.setInterval(function () { ShowCountDown(2010, 8, 10, 'showData'); }, interval);
</script>
在需要显示时间的位置加上如下代码:
复制代码 代码如下:
<div id="showData">
</div>
【Javascript倒计时代码】相关文章:
★ JavaScript基于setTimeout实现计数的方法
★ Javascript中setTimeOut和setInterval的定时器用法
★ 理解javascript定时器中的setTimeout与setInterval