Javascript XMLHttpRequest问题 [英] Javascript XMLHttpRequest issue
问题描述
我使用以下脚本来监视是否可以定期连接到网站(在我的示例代码中为10秒)。我遇到了两个问题,任何想法如何解决它们?
I am using the following script to monitor whether I can connect to a web site in a regular interval (10 seconds in my sample code). I met with two issues, any ideas how to solve them?
-
如果一个网站非常慢并且10内没有回复秒(使PingWebSite不返回),我发现因为10秒间隔到达而将执行2秒的PingWebSite调用。我的目的是我只想要一次调用PingWebSite正在执行,如果10秒间隔到达并且之前的PingWebSite正在执行,我想阻止当前的PingWebSite执行。任何想法如何解决这个问题?
If a web site is very slow and no response within 10 seconds (making PingWebSite not return), I find 2 second call to PingWebSite will be executed because of 10 second interval arrives. My purpose is I want only one call to PingWebSite is under execution, and if 10 seconds interval arrives and previous PingWebSite is executing, I want to prevent current PingWebSite from execution. Any ideas how to solve this?
我发现一个奇怪的问题,当我连接到一个非常慢的网站时,代码路径执行到警告(连接);,然后我希望异常被抛出超时,但在我的调试中,没有抛出任何异常。任何想法如何捕获超时异常?
I find a strange issue, when I connect to a very slow web site, and code path executes to "alert("connecting");", then I expect exception to be thrown for timeout, but in my debug, no exception is thrown. Any ideas how to catch timeout exception?
这是我的代码,
var index = 0;
function setup() {
window.setInterval(PingWebSite, (10 * 1000));
}
function PingWebSite() {
var http_request = new XMLHttpRequest();
try {
http_request.open("GET", "http://www.google.com", true);
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
MonitorInformation.innerText = "http://www.google.com" + " Connection ok";
alert("ok");
}
else {
alert("fail");
}
http_request = null;
} // if if (http_request.readyState == 4)
else {
// if execute here, no exception will be thrown
alert("connecting");
}
} // end of function
http_request.send(null);
} // try
catch (e) {
alert("service is not available");
}
}
编辑1:我已按照这里的建议修改我的码。这是以下版本。新问题是索引值(例如0)将在ok / fail警告消息框之前的警报消息框中提示。我认为应该在ok / fail警告消息框后的警报消息框中提示索引值(例如0)。有什么想法吗?
EDIT 1: I have followed advice here to modify my code. Here is the below version. The new issue is index value (e.g. 0) will be prompted in alert message box before ok/fail alert message box. I think index value (e.g. 0) should be prompted in alert message box after ok/fail alert message box. Any ideas why?
var index = 0;
var http_request;
var xhrTimeout;
var chkConn;
function setup() {
chkConn = window.setInterval(PingWebSite, (10 * 1000));
}
function WebMonitorTimeout() {
http_request.abort();
alert("timeout");
index = index + 1;
}
function PingWebSite() {
http_request = new XMLHttpRequest();
http_request.open("GET", "http://www.google.com", true);
http_request.onreadystatechange = function()
{
if (http_request.readyState == 4) {
if (chkConn) { clearInterval(chkConn); }
if (http_request.status == 200) {
alert("ok");
index = index + 1;
if (xhrTimeout) { clearTimeout(xhrTimeout); }
}
else {
alert("fail");
index = index + 1;
if (xhrTimeout) { clearTimeout(xhrTimeout); }
}
http_request = null;
} //if (http_request.readyState == 4)
} // end of event function
http_request.send(null);
xhrTimeout = setTimeout("WebMonitorTimeout();", 30000);
alert(index);
chkConn = window.setInterval(PingWebSite, (30 * 1000));
}
提前感谢,
George
thanks in advance, George
推荐答案
由于浏览器安全,您无法执行跨站点XHR请求
You can't do Cross Site XHR requests because of browser security
这篇关于Javascript XMLHttpRequest问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!