1、定时器简单应用
<style type="text/css">
#box{
width: 150px
;
height: 150px
;
background: red
;
border-radius: 50%
;
text-align: center
;
line-height: 150px
;
color: white
;
font-weight: bold
;
margin: 20px
;
font-size: 30px
;
}
</style>
<button id="btn">倒计时开始
</button>
<div id="box">
5
</div>
<script type
="text/javascript">
var btn
= document
.getElementById("btn");
var box
= document
.getElementById("box");
btn
.onclick = function(){
var num
= Number(box
.innerText
);
var timer
= setInterval(function(){
num
--;
box
.innerText
= num
;
if(num
=== 0){
clearInterval(timer
);
setTimeout(function(){
box
.innerText
= "GO";
},500)
}
},1000);
}
</script
>
2、商品活动倒计时
<h1 id="showTime"></h1>
var showTime
= document
.getElementById("showTime");
var endDate
= new Date("2020/10/20 20:53:40");
var nowDate
= new Date();
var s
= parseInt(getDifTime(nowDate
,endDate
));
init(s
);
function init(s
){
if(s
<= 0){
showTime
.innerHTML
= "商品活动时间已结束";
return;
}
var hours
= s
/ 60 / 60;
var d
= parseInt(hours
/24);
var h
= parseInt((hours
/24 - d
) * 24);
var f
= parseInt(((hours
/24 - d
) * 24 - h
) * 60);
var m
= parseInt((((hours
/24 - d
) * 24 - h
) * 60 - f
) * 60);
showTime
.innerHTML
= "距离商品活动结束还剩"+d
+"天" + h
+ "时"+f
+"分"+m
+"秒";
}
var timer
= setInterval(function(){
s
--;
console
.log(s
);
if(s
<= 0){
showTime
.innerHTML
= "商品活动时间已结束";
clearInterval(timer
);
return;
}
init(s
);
},1000);
转载请注明原文地址: https://lol.8miu.com/read-2000.html