更新:
我有一个 Counter.aspx.cs
private int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
while (true)
{
counter++;
Response.Write(counter+"<br>");
Response.Flush();
System.Threading.Thread.Sleep(1000);
if (counter == 10)
break;
}
Response.Write("End");
}
还有一个 Index.aspx
<script language="javascript">
function getData() {
var url = "Counter.aspx";
loadXMLDoc(url);
}
var req = false;
function createRequest() {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
} // branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
req = false;
}
}
}
}
function loadXMLDoc(url) {
try {
if (req) {
req.abort();
req = false;
}
createRequest();
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send("");
} else {
alert('unable to create request');
}
} catch (e) {
alert(e.message);
}
}
function processReqChange() {
if (req.readyState == 3) {
try {
ProcessInput(req.responseText);
if (req.responseText.length > 3000) {
getData();
}
} catch (e) {
alert(e.message);
}
}
}
var lastDelimiterPosition = -1;
function ProcessInput(input) {
var outputZone = document.getElementById("outputZone");
outputZone.innerHTML = input;
}
</script>
<body>
<input type="button" value="Start" onclick="javascript: getData()"/>
<span id="outputZone"></span>
</body>
因此,我打开两个具有相同 URL ~/Index.aspx 的浏览器并同时单击两个浏览器上的开始按钮,我首先单击“开始”的浏览器将开始计数 1... 10 结束。然后只有在第一个浏览器完成计数后,第二个浏览器才开始计数 1... 10 结束。有没有办法让两个浏览器同时开始计数?