好的,我正在尝试制作一个 AJAX 聊天系统,每 400 毫秒轮询一次聊天数据库。该部分正在工作,其中一部分不是事件用户列表。当我尝试合并这两个请求时,前两个请求被发出,然后整个事情开始滚雪球,通常定时(12 秒)的事件用户列表请求开始每 1 毫秒更新一次,第一个请求再也不会发生。显示的是两个请求的完整 AJAX 代码:
var waittime=400;chatmsg=document.getElementById("chatmsg");
room = document.getElementById("roomid").value;
chatmsg.focus()
document.getElementById("chatwindow").innerHTML = "loading...";
document.getElementById("userwindow").innerHTML = "Loading User List...";
var xmlhttp = false;
var xmlhttp2 = false;
var xmlhttp3 = false;
function ajax_read(url) {
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
document.getElementById("chatwindow").innerHTML = xmlhttp.responseText;
setTimeout("ajax_read('methods.php?method=r&room=" + room +"')", waittime);
}
}
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
}
function user_read(url) {
if(window.XMLHttpRequest){
xmlhttp3=new XMLHttpRequest();
if(xmlhttp3.overrideMimeType){
xmlhttp3.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp3) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState==4) {
document.getElementById("userwindow").innerHTML = xmlhttp3.responseText;
setTimeout("ajax_read('methods.php?method=u&room=" + room +"')", 12000);
}
}
xmlhttp3.open('GET',url,true);
xmlhttp3.send(null);
}
function ajax_write(url){
if(window.XMLHttpRequest){
xmlhttp2=new XMLHttpRequest();
if(xmlhttp2.overrideMimeType){
xmlhttp2.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp2) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
xmlhttp2.open('GET',url,true);
xmlhttp2.send(null);
}
function submit_msg(){
nick = document.getElementById("chatnick").value;
msg = document.getElementById("chatmsg").value;
document.getElementById("chatmsg").value = "";
ajax_write("methods.php?method=w&m=" + msg + "&n=" + nick + "&room=" + room + "");
}
function keyup(arg1) {
if (arg1 == 13) submit_msg();
}
var intUpdate = setTimeout("ajax_read('methods.php')", waittime);
var intUpdate = setTimeout("user_read('methods.php')", waittime);
最佳答案
问题是在 user_read
中设置了一个计时器,该计时器在 12 秒后使用正确的 URL 运行 ajax_read
。因此,当调用此 ajax_read
时,它会获取信息并设置新的超时时间,这次在 waittime
之后调用 ajax_read
,使用 ?method=r…
。所以在 user_read
第一次超时后,它就再也不会被调用了。
仅供引用,我在本地 Web 服务器上使用 Firebug(的 Net 面板)和伪造的表单和 methods.php 观看了这个。在将 waittime
设置为 4000 并使用 .innerHTML += …
后,它变得清晰起来,导致每 4 秒 两次 调用。
index.html
(我知道,它又快又脏):
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Chat</title>
</head>
<body>
<input id="chatnick" type="text" value="Nickname"><br>
<input id="roomid" type="text" value="4"><br>
<input id="chatmsg" type="text"><br>
<div id="userwindow" style="width: 500px; height: 300px"></div><br>
<div id="chatwindow" style="width: 300px; height: 300px"></div><br>
<script src="js.js"></script>
</body>
</html>
伪造的methods.php
:
blah<br>
还要注意 xmlhttp.status
可能不是 200
的可能性。
关于php - 不同时间的双 AJAX 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3049849/