在这个程序中 - 页面应该每 2 秒更新一次。
但是运行被打断了 -Uncaught TypeError: Cannot set property 'value' of undefined
在这一行 -document.formal.input.value="Count: " + counter;
为什么会发生这种情况?怎么了?
代码:
<html>
<head>
<title>Waiting example</title>
<script>
var counter=0;
// call function after evty 2 sec
id = window.setTimeout("Update();", 2000);
function Update() {
counter++;
window.status="Count " + counter;
document.formal.input.value="Count: " + counter;
// waiting after next value
id=window.setTimeout("Update();", 2000);
}
</script>
</head>
<body>
<h1>Waiting Example</h1>
<hr>
Value in line status & page are update evry 2 sec.
Click the button Reset for launch counter from zero, and on stop for stop counter.
<hr>
<form name="formal">
<input type="text" name="input1" size="40"><br>
<input type="button" value="RESET" onClick="counter=0;"><br>
<input type="button" value="STOP" onClick="window.clearTimeout(id);"><br>
<hr>
</body>
</html>
问题:
- 如何解决这个麻烦?
- 为什么会这样?