js简单实现时间格式倒计时

在网上找了很多相关的例子,总觉得还是要根据需求来写不一样的代码,就如我只是想要实现一个时间格式的倒计时,就不需要什么插件了,简短的一段代码即可实现。

看代码了:

<script language="javascript" type="text/javascript">
<!--
var maxtime = 7200;//按秒计算,自己调整! by hkshadow QQ 2765237
function CountDown(){
 if(maxtime >= 0){
 seconds = maxtime--;
 	//var d = parseInt(seconds/86400);
 	var h = parseInt((seconds%86400)/3600);
	var m = parseInt((seconds%3600)/60);
	var s = parseInt(seconds%60);
	
	var h = h > 0 ? h : "00";
	var m = m > 0 ? m : "00";
	var s = s > 0 ? s : "00";
	
 	//msg = d+"天"+h+"小时"+m+"分钟"+s+"秒";  //倒计时
  	//msg = ""+h+":"+m+":"+s+":";  //倒计时
	//var date_array = [h,m,s];
 	document.getElementById('hour').innerHTML=h;
 	document.getElementById('minute').innerHTML=m;
  	document.getElementById('second').innerHTML=s;
}
 else{
		clearInterval(second);
		//P.cancel();
	// top.location = "/index.php";
	 }
}
setInterval("CountDown()",1000);
//-->
</script>

html页面代码:

<span class="thespan1" id="hour">..</span>
<span class="thespan2">:</span>
<span class="thespan1" id="minute">..</span>
<span class="thespan2">:</span>
<span class="thespan1" id="second">..</span>

最后的效果是
1:24:50

Leave a Reply

(will not be published)