html<pre>
<span endTime="1472169600" class="start_time"></span>
</pre>
jquery
<pre>
<script
function timer(){
window.setInterval(function(){
$('.start_time').each(function(i){//循环每一个类
console.log(this);
var start_time = $(this).attr("endTime"); //获得当前这个类中的结束时间戳 秒
var currentTime = new Date().getTime();//获得当前的时间戳 毫秒
var intDiff = start_time - Math.round(new Date().getTime()/1000-28800);//把当前时间戳毫秒级转换成秒级进行加减
var day=0,
hour=0,
minute=0,
second=0;//时间默认值
if(intDiff > 0){
day = Math.floor(intDiff / (60 * 60 * 24));
hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
}
if (minute <= 9) minute = '0' + minute;
if (second <= 9) second = '0' + second;
$(this).html(day+"天"+hour+'时'+minute+'分'+second+'秒');//输出
});
}, 1000);//每一秒执行一次
}
$(function(){
timer();
});
</script>
</pre>
网友评论