本文实例为大家分享了JS实现秒杀倒计时特效的具体代码,供大家参考,具体内容如下
知识点
添加一个定时器,对时间标签不断进行更新设置即可。
引入工具库工具库
运行效果
代码
引入MyTool.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
font-size: 30px;
}
span{
background-color: #000;
color: #fff;
border-radius: 10px;
padding: 0 5px;
}
</style>
</head>
<body>
<div id="box">
<span>00</span>:<span>00</span>:<span>00</span>
</div>
<script src="../00MyTools/MyTools.js"></script>
<script>
window.addEventListener('load',function (ev) {
var hour = myTool.$('box').children[0], min = myTool.$('box').children[1], sec = myTool.$('box').children[2];
var time = 8 * 60 * 60;
var timer = setInterval(function () {
time--;
hour.innerText = myTool.addZero(myTool.secondToHourMinSecond(time).hour);
min.innerText = myTool.addZero(myTool.secondToHourMinSecond(time).min);
sec.innerText = myTool.addZero(myTool.secondToHourMinSecond(time).second);
if (time===0){
clearInterval(timer);
}
},1000);
},false);
</script>
</body>
</html>