javascript四则运算与实时显示更新时间
javascript四则运算与实时显示更新时间,代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒计时</title>
</head>
<body>
<form action="js.html" id="myForm" name="myForm" method="post">
</form>
<div>
<input type="text" name="num" id="numDao" onblur="setText1()" size="5:px"/>
<samp id="timenum"></samp> </div>
<input type="button" name="but" id="but1" value="开始" onclick="beginTime()"/>
<input type="button" name="but" id="but2" value="暂停" onclick="endTime()"/>
<input type="button" name="but" id="but3" value="重置" onclick="reset()"/>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
document.getElementById("but1").disabled="false";
document.getElementById("but2").disabled="false";
document.getElementById("but3").disabled="false";
}
var timeConstr = null;
var sec = null;
function reset(){
var flag = window.confirm("你确定要重置吗?");
if(flag){
window.clearTimeout(timeConstr);
document.getElementById("numDao").removeAttribute("disabled");
document.getElementById("timenum").innerHTML="";
document.getElementById("numDao").value="";
document.getElementById("but1").disabled="false";
document.getElementById("but2").disabled="false";
document.getElementById("but3").disabled="false";
}
}
function setText1(){
var timenum = document.getElementById("numDao").value;
if(timenum.length!=0){
if(isNaN(timenum)){
alert("请输入数字");
document.getElementById("numDao").focus();
}else{
document.getElementById("numDao").disabled="false";
document.getElementById("but1").removeAttribute("disabled");
sec=timenum*60;
}
}else{
alert("请输入内容");
}
}
function beginTime(){
var day = Math.floor(sec/3600/24);
var hour = Math.floor((sec/3600)-24*day);
var minu = Math.floor((sec-hour*3600-24*day*3600)/60);
var second = sec-minu*60-hour*3600-24*day*3600;
if(minu>0 && minu<10){
minu = "0"+minu;
}
if(second>0 && second<10){
second = "0"+second;
}
document.getElementById("timenum").innerHTML="剩余:"+day+"天"+hour+"小时"+minu+"分钟"+second+"秒";
if(sec==0){
alert("表单提交了");
window.clearTimeout(timeConstr);
document.forms.item(0).submit();
}
document.getElementById("but1").disabled="false";
document.getElementById("but2").removeAttribute("disabled");
document.getElementById("but3").removeAttribute("disabled");
timeConstr = window.setTimeout('beginTime()',1000);
sec=sec-1;
}
function endTime(){
window.clearTimeout(timeConstr);
document.getElementById("but1").removeAttribute("disabled");
document.getElementById("but2").disabled="false";
}
</script>
希望对你有所帮助,如有问题欢迎留言。