1

在这个程序中 - 页面应该每 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>

问题:

  • 如何解决这个麻烦?
  • 为什么会这样?
4

3 回答 3

0

采用

document.formal.input1.value="Count: " + counter;

LIVE DEMO

于 2013-04-07T07:18:14.827 回答
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;
}    

示例:http: //jsfiddle.net/bLet2/

于 2013-04-07T07:26:48.730 回答
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>
于 2013-04-07T08:32:03.567 回答