JavaScript 毫秒级别
显示格式 天数 : 小时 : 分钟 : 秒
<div class="time-item">
<span id="day_show">0天</span>
<strong id="hour_show">0时</strong>
<strong id="minute_show">0分</strong>
<strong id="second_show">0秒</strong>
<strong id="ms_show">0毫秒</strong>
</div>
<script type="text/javascript">
function timer() {
var iCount=setInterval(function (){ // 将定时器赋给变量 方便在清除定时器时 以句柄的形势清除
var myDate = new Date();
var beginDate = (new Date("2018/06/29 16:23:21")).getTime(); //得到毫秒
var nowDate = myDate.getTime()
var between = beginDate-nowDate
console.log(between)
var s = parseInt(between/1000)
console.log(s)
if (s > 0) {
var day = Math.floor(s/3600/24)
var hours = Math.floor(s/3600) - (day*24)
var min = Math.floor(s/60) - (day*24*60) - (hours*60)
var second = Math.floor(s) - (day*24*3600) - (hours*3600) - (min*60)
var ms = Math.floor(between) - (day*24*3600*1000) - (hours*3600*1000) - (min*60*1000) - (second*1000)
$('#day_show').html(day + "天");
$('#hour_show').html('<s id="h"></s>' + hours + '时');
$('#minute_show').html('<s></s>' + min + '分');
$('#second_show').html('<s></s>' + second + '秒');
$('#ms_show').html('<s></s>' + ms + '秒');
}else{
$('#day_show').html(0 + "天");
$('#hour_show').html('<s id="h"></s>' + 0 + '时');
$('#minute_show').html('<s></s>' + 0 + '分');
$('#second_show').html('<s></s>' + 0 + '秒');
$('#ms_show').html('<s></s>' + 0 + '秒');
// setTimeout("stop()", 10);
clearInterval(iCount) // 清除定时器 附带句柄条件
}
},10)
}
/**
* 此处还可以编写一个保护设备 防止长期停留在页面导致内存不足的情况
* 高频的定时器 保护的方法限制停留时间越小越好
*/
$(function() {
timer();
});
</script>
扫描二维码关注公众号,回复:
1839806 查看本文章