长轮询将保持页面加载(javascript问题)

时间:2011-10-19 16:26:55

标签: javascript jquery long-polling

我使用长轮询构建了一个聊天脚本,但我不明白为什么它会继续加载。长轮询应在提交文本后开始,但看起来它会自动启动。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Helpdesk Longpolling Test</title>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    <script type="text/javascript" charset="utf-8">

    var timestamp = null;
    var name = "Guest: ";

    function sendReply(message, myAsync) {
        $.ajax({
            type: "GET",
            url: "putData.php?text=" + message,
            async: myAsync,
            cache: false,

            success: function(data) {
                var json=eval('('+data +')');
                    //alert(json["code"]);
                if (json["code"] == "500") {
                    $('div#chatbox').append('Error<br />');
                } else if (json["code"] == "200") {
                    $("#textToChat").val("");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $('div#chatbox').append('<p>Error: ' + textStatus + ' (' + errorThrown + ')</p>');
                //alert("error: " + textStatus + " ("+errorThrown + ")");
            }
        });
    }

    function waitForMsg() {
        $.ajax({
            type: "GET",
            url: "getData.php?timestamp=" + timestamp,
            async: true,
            cache: false,

            success: function(data) {
                var json=eval('('+data +')');
                if (json["msg"] != "") {
                    //alert(json["msg"]);
                    $('div#chatbox').append(' ' + json["msg"] + '<br />');
                }
                timestamp = json["timestamp"];
                setTimeout('waitForMsg()', 1000);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $('div#chatbox').append('<p>Error: ' + textStatus + ' (' + errorThrown + ')</p>');
                alert("error: " + textStatus + " ("+errorThrown + ")");
                //setTimeout('waitForMsg()', 1000);
            }
        });
    }

    $(window).load(function() {

        $('#sendMessage').submit(function() {

            sendReply(name+" is connected.", true);
            setTimeout(function() { 
              waitForMsg();
              openConnection = true;
            }, 500);

            var text = $("#textToChat").val();
            sendReply(name+": "+text, true);

          return false;
        });

    });

    $(window).bind('beforeunload', function(){ 
        sendReply(name+" has left the conversation.", false);
    });
</script>
  </head>

  <body>
  <div id="chatbox">
  <h3><a href="" id="StartSupport">Live Support!</a></h3>
  </div>
  <form method="get" id="sendMessage">
    <input id="textToChat" name="text" type="text" value="Type your question..." size="63" />
    <input name="submitmsg" type="submit" id="submitmsg" value="Verzenden" />  
  </form>

    <input type="button" id="actie" name="actie" value="Actie_in_CMS" />
  </body>

</html> 

有想法的人?我想我犯了一个愚蠢的错误,但是在试图调试脚本2天之后无法弄明白。加载页面后,脚本运行正常。

0 个答案:

没有答案