简介:JavaScript实现简单的倒计时功能
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>JavaScript-倒计时(一)</title>
<script type="text/javascript">
var NowTime=new Date();
var setMin=0;
function Time(){
var min=document.getElementById("shi").value;
var setMin=min*60*1000;
var EndTime=NowTime.getTime()+setMin;
var sTime=EndTime-new Date().getTime();
var h=0;
var m=0;
var s=0;
if(sTime>=0){
//时分秒的计算方法(关键点)
h=Math.floor(sTime/1000/60/60%24);
m=Math.floor(sTime/1000/60%60);
s=Math.floor(sTime/1000%60);
}
//document.write(h+":"+m+":"+s);
document.getElementById("t_h").innerHTML = check(h) + "时";
document.getElementById("t_m").innerHTML = check(m) + "分";
document.getElementById("t_s").innerHTML = check(s) + "秒";
}
// function Ji(){
setInterval(Time,0);//关键点
//}
function check(e){
if(e<10){
e="0"+e;
return e;
}else{
return e;
}
}
</script>
</head>
<body>
<input type="text" value="0" id="shi" style="height:30px;width:45px;text-align:center;"/>分钟<br/>
<!--<button onclick="Ji()" style="height:30px;">计时</button>-->
<div id="show">
<span id="t_h">00时</span>
<span id="t_m">00分</span>
<span id="t_s">00秒</span>
</div>
</body>
</html>