May not be the right forum for asking this question, but I dont know where should I post it. Please point my the right direction if it is not the right place.
I wrote a simple "Ajax" app. and it does not work as I expected.
There are 3 <span> elements, click on each on them will initiate a httprequest to server. The server will execute code that suspend for 5 seconds and then response a simple text to client. Once the page received the response, print that response to the page. Simple. If I click the <span> one by one in 1 second interval, what I expected is that the first response text will appear in 5 seconds later, and the second will appear in the 6th second and so on. However what it come out is the first respone in the 5th second, the second in the 10th second, and the third one in the 15th second. What's wrong with my code.
Test.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <script> function createRequestObject() { var xmlhttp; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlhttp = null; } } if (!xmlhttp && typeof XMLHttpRequest != "undefined") { xmlhttp = new XMLHttpRequest(); } return xmlhttp; } function sendRequest(param, resultid) { var http = createRequestObject();
document.getElementById(resultid).innerText = "waiting..."; try { var passparam = escape(param); http.open("GET", "save.asp p="+passparam, true); http.setRequestHeader("Content-Type", "text/xml"); http.onreadystatechange = function(){ handleResponse(http, resultid); } http.send(null); } catch (e) { alert("Request send failed."); } finally{} } function handleResponse(http, resultid) { try { if ((http.readyState == 4) && (http.status == 200)) { document.getElementById(resultid).innerText = http.responseText; } } catch (e) { } } </script> <span onclick="sendRequest('Save1', 'result1');">Save1</span><br /> <span onclick="sendRequest('Save2', 'result2');">Save2</span><br /> <span onclick="sendRequest('Save3', 'result3');">Save3</span><br />
<span id="result1"></span><br /> <span id="result2"></span><br /> <span id="result3"></span><br /> </BODY> </HTML>
Save.asp:
<% Response.Expires=-1 StartTime=Now() Do While Now() < StartTime + 5.0 / (3600.0 * 24.0) Loop Response.Write "Saved" & Request("p") %>
Thanks in advanced.
Jason
.NET Development23
|