<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
var num = 1;
var i;
function startCount() {
document.getElementById('count').value = num;
num = num + 1;
i = setTimeout("startCount()", 1000);
console.log("1号:"+num);
}
var flag = 0;
function controlStart() {
if (flag == 0) {flag = 1;
num--;startCount();
}
else { }
}
function stopCount() {
clearTimeout(i);
flag = 0;
}
function clear(){
if (flag ==0){
num = 0;
document.getElementById("count").value = num ;
}
else {}
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="Start" onclick="controlStart()" />
<input type="button" value="Stop" onclick="stopCount()" />
<input type="button" value="clear" onclick="clear()" />
</form>
</body>
</html>
为什么num无法清零?代码如上面,clear()函数里面设置了num=0,但是仍然无法清零。