javascript - 每 2 秒更新一次页面 - 未捕获的类型错误

标签 javascript runtime-error

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

问题:

  • 如何解决这个问题?
  • 为什么会发生这种情况?

最佳答案

我解决了你的问题。将行更改为此 - 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>

关于javascript - 每 2 秒更新一次页面 - 未捕获的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15859827/

相关文章:

javascript - 如何 console.log 此 JSON 对象的一个​​值?

javascript - Javascript 中函数关键字的混淆

javascript - 找不到字体,vuejs构建

javascript - ADF 根据 OutputText 值显示图像

javascript - 为什么我的程序使计算机看起来三 Angular 学表现不佳?

syntax - 鸡计划是否支持复数?如果是这样,为什么我会收到此错误?

JNI 的 Java 运行时错误

ruby-on-rails - 参数超出范围 Rails 4 和 bootstrap3-datetimepicker-rails

google-cloud-platform - 在 Debian 10 中安装 Google SDK?

c++ - 运行时错误 : Template variable