在此程序中 - 页面应每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>
问题:
答案 0 :(得分:0)
答案 1 :(得分:0)
请改用setInterval。
var counter=0;
// call function after evty 2 sec
id = window.setInterval(Update, 2000);
function Update() {
counter++;
window.status="Count " + counter;
document.formal.elements["input1"].value="Count: " + counter;
}
答案 2 :(得分:0)
我解决了你的麻烦。将行更改为此 - document.form1.input1.value
这是代码:
<html>
<head><title>Timeout Example</title>
<script>
var counter = 0;
// call Update function in 2 seconds after first load
ID=window.setTimeout("Update();",2000);
function Update() {
counter++;
window.status="The counter is now at " + counter;
document.form1.input1.value="The counter is now at " + counter;
// set another timeout for the next count
ID=window.setTimeout("Update();",2000);
}
</script>
</head>
<body>
<h1>Timeout Example</h1>
<hr><p>
The text value below and the status line are being updated every two seconds.
Press the RESET button to restart the count, or the STOP button to stop it.
</p><hr>
<form NAME="form1">
<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);">
</form>
<hr>
</body>
</html>