Javascript 每2秒更新一次页面-未捕获类型错误
在此程序中,页面应每2秒更新一次。 但运行被-uncaughttypeerror中断:无法设置未定义的属性“value” 在这一行-document.formal.input.value=Count:+counter; 为什么会发生这种情况?怎么了 代码: 问题: 你如何解决这个问题? 为什么会这样? 使用Javascript 每2秒更新一次页面-未捕获类型错误,javascript,runtime-error,Javascript,Runtime Error,在此程序中,页面应每2秒更新一次。 但运行被-uncaughttypeerror中断:无法设置未定义的属性“value” 在这一行-document.formal.input.value=Count:+counter; 为什么会发生这种情况?怎么了 代码: 问题: 你如何解决这个问题? 为什么会这样? 使用 改用setInterval var counter=0; // call function after evty 2 sec id = window.setInterval(Update,
改用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;
}
示例:我解决了您的问题。将行更改为-document.form1.input1.value 以下是代码:
为什么会这样?我是Java脚本的初学者。你能显示我做错了什么吗?因为你需要用名字formal:input1来指出表单输入的名字
document.formal.input1.value="Count: " + counter;
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;
}
<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>