<body>
<div>
<!-- 页面的块 -->
<span id="days"></span>
<span id="hour"></span>
<span id="minute"></span>
<span id="second"></span>
</div>
<script>
// 调用函数
countdown();
//封装函数
function countdown() {
var date = new Date();
// 将来要到的那个时间
var jTime = +new Date('2020/11/11 12:00:00');
// 获取到元素
var days = document.getElementById("days");
var hour = document.getElementById("hour");
var minute = document.getElementById("minute");
var second = document.getElementById("second");
var newTime = +new Date();
var allSecond = (jTime - newTime) / 1000;//转换秒数
var d = parseInt(allSecond % 60);//秒数
d = d < 10 ? '0' + d : d;// 一位数的时候个位前+0 两位数输出原数
var e = parseInt(allSecond / 60 % 60);//分钟
e = e < 10 ? '0' + e : e;
var f = parseInt(allSecond / 60 / 60 % 24);//小时
f = f < 10 ? '0' + f : f;
var g = parseInt(allSecond / 60 / 60 / 24);//天
g = g < 10 ? '0' + g : g;
// 在页面元素出现
days.innerText = g + '天';
hour.innerText = f + '时';
minute.innerText = e + '分';
second.innerText = d + '秒';
}
setInterval(countdown, 1000);
</script>
</body>