processing multiple AJAX requests
i am new to AJAX
The problem i am facing is that i have a database query that takes 2 minutes to execute...while it executing 开发者_如何学Pythoni want the user to get an update of whats happening ie every 30 sec get the number of order processed from database.
before i start the order processing query..i print the order staus..works fine..then i start teh order processing query and in between if i hit the update button..it waits fro the order processing to complete then gives me update... here is the code i have
<html>
<head>
<script type="text/javascript">
function getupdate()
{
v=document.getElementById('test').value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{alert(v);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("update").innerHTML=v;
//document.getElementById("chunks").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","querygoogle.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.send("process=9");
}
function chunks()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp1=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp1.onreadystatechange=function()
{
if (xmlhttp1.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chunks").innerHTML=xmlhttp1.responseText;
}
}
xmlhttp1.open("POST","querygoogle1.php",true);
xmlhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp1.send("process=0");
//xmlhttp.send();
}
</script>
</head>
<body >
<span id="txtHint"></span><input name="test" type="text" id="test" onchange="getupdate();" value="3"/>
<p>Status: </p>
<div id="chunks" >
<p>Being Submitted for Analysis</p>
<p>
<input name="search" type="button" value="search" onclick="chunks();">
</p>
</div>
<p>Update: </p>
<div id="update" ></div>
</body>
</html>
can anyone pls help. Thanks
Sounds like your problem is server side rather than client side. If I understand you correctly you want the flow to be:
- Client sends request for database query.
- Client requests update.
- Update is received.
- Client requests update.
- Update is received. (repeat until...)
- Query result is received.
The actions you do onreadystatechange
are asynchronous, so the client interface is just sitting around waiting for the ajax call to return from the server - and if that isn't happening until the processing is done, that means your requests are stalling on the server.
Are you sure the server side of the request update returns promptly with the status, or is it a chance it's stalled until the processing is finished?
Yes, it certainly seems that it's a server-side problem. It seems that the server cannot process your update request because it's running the DB query.
精彩评论