倒计时抢购(JS时间函数)

这里我将时间设置到了2019年3月25日
这里我模拟时间已经到了的时候
time.html

<!doctype html>
<html >
 <head>
 <meta charset="UTF-8">
  <title>京东倒计时抢购</title>
  <style>
  *{
  margin:0;
  padding:0;
  font-size:20px;
  
  }
  .box{
  text-align:center;
  margin:20px auto;
  width:300px;
  height:50px;
  line-height:50px;
  border:1px solid red;
  }
  .box span{
  color:yellow;
  font-weight:bold;
  }
  </style>
 </head>
 <body>
 <div class="box">距离开抢还有:<span id="timeBox">00:00:00</span></div>
  <script src="time.js" charset="utf-8"></script>
 </body>
</html>

time.js

var timeBox=document.getElementById('timeBox');
function computed(){
	var nowTime=new Date(),
		targetTime=new Date('2019/3/1 16:00:00');
	var spanTime=targetTime-nowTime;
	if(spanTime<=0){
		timeBox.innerHTML="开始抢购";
		window.clearInterval(timer);
		return;
	}
	var hour=Math.floor(spanTime/(1000*60*60));
	spanTime-=hour*1000*60*60;
	var minute=Math.floor(spanTime/(1000*60));
	spanTime-=minute*1000*60;
	var second=Math.floor(spanTime/1000);
	hour<10?hour="0"+hour:null;
	minute<10?minute="0"+minute:null;
	second<10?second="0"+second:null;
	timeBox.innerHTML=hour+":"+minute+":"+second;
}
computed();
var timer=window.setInterval(computed,1000);;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值