<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<input type="text" value="00:00"/> | |
<input type="button" value="开始"/> | |
<input type="button" value="暂停"/> | |
<input type="button" value="重置"/> | |
<script type="text/javascript"> | |
var otxt=document.getElementsByTagName("input")[0]; | |
var oStart=document.getElementsByTagName("input")[1]; | |
var oStop=document.getElementsByTagName("input")[2]; | |
var oReset=document.getElementsByTagName("input")[3]; | |
var n=0; | |
var timer=null; | |
//开始计时: | |
oStart.οnclick=function(){ | |
clearInterval(timer); | |
timer=setInterval(function(){ | |
n++; | |
var m=parseInt(n/60); | |
var s=parseInt(n%60); | |
otxt.value=zeroFill(m)+":"+zeroFill(s); | |
},1000/60); | |
} | |
//重置: | |
oReset.οnclick=function(){ | |
otxt.value="00:00"; | |
n=0; | |
} | |
//暂停: | |
oStop.οnclick=function(){ | |
clearInterval(timer); | |
}; | |
//补零: | |
function zeroFill(n){ | |
return n<10?"0"+n:""+n; | |
} | |
</script> | |
</body> | |
</html> | |