# js实现时分秒的定时器

278人阅读 评论(0)
<p class="fr"><span class="time" id="time">02:11:10</span> 后竞猜截止</p>

js代码
/*倒计时start*/
var interval = 1000;
var leftSecond;
var time = $("#time").html(); var arr = time.split(":");//以冒号分组 var hour = arr[0]; var minutes = arr[1]; var second = arr[2]; var hLength = hour.length; var mLength = minutes.length; var sLength = second.length; if(hLength === 2){ if(hour.substring(0,1) === '0'){ hour = sub(hour); } } if(mLength === 2){ if(minutes.substring(0,1) === '0'){ minutes = sub(minutes); } } if(sLength === 2){ if(second.substring(0,1) === '0'){ second = sub(second); } } function sub(num){ return num.substring(1,2);//截取第二个位置的 } hour = parseInt(hour); minutes = parseInt(minutes); second = parseInt(second); leftSecond = hour*60*60 + minutes*60 + second; function showTime(h , m , s){ leftSecond --; if(leftSecond === 0){$("form").attr('action',"javascript:;")
}
var h = Math.floor(leftSecond/3600);
var m = Math.floor((leftSecond-h*3600)/60);
var s = Math.floor(leftSecond-h*3600 - m*60);
if(s < 10){
s = '0' + s;
}
if(m < 10){
m = '0' + m;
}
if(h < 10){
h = '0' + h;
}
\$("#time").html(h+":"+ m +":" + s);

}

window.setInterval(function(){
showTime(hour , minutes , second );
},interval)
/*倒计时end*/
0
0

个人资料
• 访问：55553次
• 积分：1203
• 等级：
• 排名：千里之外
• 原创：66篇
• 转载：0篇
• 译文：1篇
• 评论：1条
文章分类
阅读排行