Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest中断文本字段中的键入_Javascript_Ajax_Xmlhttprequest - Fatal编程技术网

Javascript XMLHttpRequest中断文本字段中的键入

Javascript XMLHttpRequest中断文本字段中的键入,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我已经使用XMLHttpRequest编写了一个小型聊天应用程序。 调用这三行将中断键入 xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","http://mydomain.com/chat?action=getLatestEntries&latestID="+latestID, false); xmlhttp.send(null); 如果用户在文本字段中键入内容,XMLHttpRequest调用将中断键入1或2秒 你知道我该怎么解决

我已经使用XMLHttpRequest编写了一个小型聊天应用程序。 调用这三行将中断键入

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://mydomain.com/chat?action=getLatestEntries&latestID="+latestID, false);
xmlhttp.send(null);
如果用户在文本字段中键入内容,XMLHttpRequest调用将中断键入1或2秒

你知道我该怎么解决这个问题吗

完整代码如下所示:

<script type="text/javascript">
var latestID = 0; // global
//This function will display the messages
function showmessages(){
   //Send an XMLHttpRequest 
   if(window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET","http://mydomain.com/chat?action=getLatestEntries&latestID="+latestID, false);
      xmlhttp.send(null);
   }
   else{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open("GET","http://mydomain.com/chat?action=getLatestEntries&latestID="+latestID, false);
      xmlhttp.send();
   }   
   //Replace the content of the messages with the response from the .py file
   elem_messages = document.getElementById("messages");
   if (elem_messages) {
     elem_messages.innerHTML = xmlhttp.responseText;
   }

   // put scrollbar to bottom   
   elem_chat = document.getElementById("chat");
   if (elem_chat) {   
     elem_chat.scrollTop = document.getElementById("scroll_down").offsetTop + 5000;        
   } 
   //Repeat the function each 5 seconds
   setTimeout('showmessages()',5000);
}
//Start the showmessages() function
showmessages();
//This function will submit the message
</script>


<div id="chat" style="height: 450px; overflow: auto; width: 700px;">
    <div id="messages"></div>    
    <span id="scroll_down" />      
</div>

<div id="write">
    <form name="chatform" id="chatform" method="post" action="" onsubmit="send();return false"
          style="margin-top: 10px;">
        <p>Message:</p>
        <input type="text" id="message" size="90" /><br/>
        <div style="margin-top:6px"><input type="button" value="Send" onClick="send();" /></div>        
    </form>
</div>

var latestID=0;//全球的
//此功能将显示消息
函数showmessages(){
//发送XMLHttpRequest
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
open(“GET”http://mydomain.com/chat?action=getLatestEntries&latestID=“+最新,错误);
xmlhttp.send(空);
}
否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
open(“GET”http://mydomain.com/chat?action=getLatestEntries&latestID=“+最新,错误);
xmlhttp.send();
}   
//用.py文件中的响应替换消息的内容
elem_messages=document.getElementById(“messages”);
如果(元素信息){
elem_messages.innerHTML=xmlhttp.responseText;
}
//将滚动条放到底部
elem_chat=document.getElementById(“chat”);
if(elem_chat){
elem_chat.scrollTop=document.getElementById(“向下滚动”).offsetTop+5000;
} 
//每5秒钟重复一次该功能
setTimeout('showmessages()',5000);
}
//启动showmessages()函数
showmessages();
//此函数将提交消息
信息:



注意:此处未列出send()函数,以保持帖子的小而干净。

这是因为您使用的同步请求会锁定浏览器。您需要将其更改为异步请求

非常感谢你的回答。如何将其转换为异步请求?使用onreadystatechange并在open()方法上将false更改为true。我尝试了此方法,但无效:
xmlhttp=new XMLHttpRequest();xmlhttp.onreadystatechange=function(){xmlhttp.open(“GET”,“http://mydomain.com/chat?action=getLatestEntries&latestID=“+latestID,true);xmlhttp.send(null);}
容器“messages”是空的,没有显示聊天消息。我想,我知道了<代码>//如果(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();xmlhttp.open(“GET”http://mydomain.com/chat/?action=getLatestEntries&latestID=+latestID,true);xmlhttp.onload=function(e){if(xmlhttp.readyState==4){if(xmlhttp.status==200){//用.py文件elem_messages.innerHTML=xmlhttp.responseText;}}}xmlhttp.send(null);