奇怪的矛盾错误

时间:2015-06-16 00:59:20

标签: javascript ajax

我有一个带ruby的heroku网站,但我的问题是特别是一页。该页面的问题是javascript。该页面上还有ajax。这是我的页面:

<!DOCTYPE html>
    <html>
        <head>
            <script>
            var refreshDelay = 5000000;
            function createRequestObject() {
                var ro;
                if(navigator.appName == "Microsoft Internet Explorer"){
                    ro = new ActiveXObject("Microsoft.XMLHTTP");
                }else{
                    ro = new XMLHttpRequest();
                }
                return ro;
            }
            var http = createRequestObject();
            function sndReq() {
                var newParent = document.getElementById('2');
                var oldParent = document.getElementById('target');

                while (document.getElementById('target').childNodes.length > 0) {
                    newParent.appendChild(document.getElementById('target').childNodes[0]);
                }
                http.open('post', '/chatContent?n=<%=@name%>');
                http.onreadystatechange = handleResponse;
                http.send(null);
            }
            function handleResponse() {
                var newParent = document.getElementById('2');
                var oldParent = document.getElementById('target');

                while (document.getElementById('target').childNodes.length > 0) {
                    newParent.appendChild(document.getElementById('target').childNodes[0]);
                }
                if(http.readyState == 4){
                    var response = http.responseText;
                    document.getElementById('target').innerHTML = response;
                    setTimeout(sndReq(), refreshDelay);
                }
            }
            setTimeout(sndReq(), refreshDelay);
            </script>
            <script>
            scrollDown = function() {
               document.body.scrollTop = document.body.scrollHeight;
            }
            </script>
        </head>
        <body onload='scrollDown()'>
            <div id='2'>
            </div>
            <div id='target'>
                <%=@chat%> <!-- @chat is a variable from my ruby file -->
            </div>
            <form action="/addChat?n=<%=@name%>" method='post'>
                <input name='nchat' type='text' autofill='no' style='width:100%;height:10em;vertical-align:top'>
                <input type='submit'>
            </form>
            <a href='/home'>Go home!</a>
        </body>
    </html>

当我加载页面时,它在控制台中给出了关于第24行的错误:

Uncaught TypeError: Cannot read property 'childNodes' of null

但是当我进入控制台document.getElementById('target').childNodes.length时,它给了我很多节点(动态变化)。发生了什么事?

你想看到任何额外的东西来回答这个问题,我会尽快发布。请问!

1 个答案:

答案 0 :(得分:3)

您正在调用setTimeout(sndReq(), refreshDelay);,因为您将该功能传递给sndReq(),所以会立即执行setTimeout

由于您的sndReq()在您脑海中,因此HTML尚未完全加载,因此您收到选择器错误,因为该元素尚未存在(还)。

您可以将setTimeout(sndReq(), refreshDelay);更改为setTimeout(sndReq, refreshDelay);以将函数引用传递给setTimeout,以便sndReq()不会立即触发。

参考:setTimeout