<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" id="start" value="开始计时!" />
<input type="text" id="time" value="" />
<input type="button" id="clear" value="清除计时!" />
<script type="text/javascript">
var a = 0;
var flag = false;
var b = document.getElementById("time");
var c;
document.getElementById("start").onclick = function() {
if (!flag) {
flag = true;
document.getElementById("start").value = "停止计时!";
time();
} else {
document.getElementById("start").value = "开始计时!";
timea();
}
}
function time() {
b.value = a;
a = a + 1;
c = setTimeout(time, 1000);
}
function timea() {
clearTimeout(c);
flag = false;
}
document.getElementById("clear").onclick = function() {
clear_time();
}
function clear_time() {
clearTimeout(c);
flag = false;
a = 0;
b.value = a;
}
</script>
</body>
</html>