jQuery 实现倒计时天,时,分,秒功能
1.HTML部分
<span class="joind">135</span>天<span class="joinh">7</span>时<span class="joinm">46</span>分<span class="joins">25</span>秒
2.js部分
<script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var setTimer = null; var chazhi = 0; //差值计算 //例子(模拟) chazhi = 135 * 86400000; //真实时间(注意月份需减掉1,否则时间会计算错误) //chazhi = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数 //chazhi = (new Date(2018,8-1,6,6,6,6)) - (new Date()); //执行函数部分 countFunc(chazhi); setTimer = setInterval(function() { chazhi = chazhi - 1000; countFunc(chazhi); }, 1000); function countFunc(leftTime) { if(leftTime >= 0) { var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟 var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数 days = checkTime(days); hours = checkTime(hours); minutes = checkTime(minutes); seconds = checkTime(seconds); $(".joind").html(days); $(".joinh").html(hours); $(".joinm").html(minutes); $(".joins").html(seconds); } else { clearInterval(setTimer); $(".joind").html("00"); $(".joinh").html("00"); $(".joinm").html("00"); $(".joins").html("00"); } } function checkTime(i) { //将0-9的数字前面加上0,例1变为01 if(i < 10) { i = "0" + i; } return i; } </script>
3.说明
如果变为真实时间月份需要减1,否则时间差计算会有错误
总结
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17