<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div{
width: 800px;
height: 100px;
line-height: 100px;
margin: 0 auto;
margin-top: 100px;
color: #000;
font-size: 26px;
}
</style>
</head>
<body>
<div class="txt"></div>
</body>
<script type="text/javascript">
let strTime = new Date('2020/1/18 00:00:00').getTime();
let endTime = new Date('2019/11/23 17:09:00').getTime();
//格式化时间
function num(n){
return n < 10 ?'0'+n :n;
};
let Interval = (strTime,endTime)=>{
let now = new Date().getTime();//当前时间
if(strTime>now){
console.log('距离活动开始还有:')
timerfunc(strTime,'距离活动开始')
}else{
console.log('距离活动结束还有:')
timerfunc(endTime,'距离活动结束')
}
}
//倒计时函数
let timerfunc = (time,type)=>{
let timer = setInterval(function(){
//当前时间
var startTime = new Date().getTime();
//结束时间
var endTimes = time;
//算出中间差,以毫秒数返回.
var countDown = (endTimes-startTime);
//获取天数
var oDay = parseInt(countDown/1000/60/60/24);
//获取小时数
var oHour = parseInt(countDown/1000/60/60%24);
//获取分钟数
var oMinute = parseInt(countDown/1000/60%60);
//获取秒数
var oSecond = parseInt(countDown/1000%60);
//获取标签
var txt = document.querySelector('.txt');
//输出时间
if(oDay<0 || oHour<0 || oMinute<0 || oSecond<0) {
clearInterval(timer);
type == '距离活动结束'?console.log('活动已结束!'):Interval(strTime,endTime)
return;
}
var t = num(oDay)+ '天'+num(oHour)+'时'+num(oMinute)+'分'+num(oSecond)+'秒';
console.log(t)
//txt.innerHTML = t;
},1000) ;
}
Interval(strTime,endTime)
</script>
</html>
活动倒计时 (距离活动开始还有多少时间/距离活动结束还有多少时间)
最新推荐文章于 2021-10-29 17:33:05 发布