我们的想法是在循环中启动多个xmlhttp请求,并让它们将结果存储在全局变量中。
for (var i = 0; i < something; i++) {
fetch_site(url, i);
}
现在函数fetch_site()看起来像这样:
// the result will be palced at the i-th field in the "content"-array
function fetch_site(url, i) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
// Milliseconds a request can take before automatically being terminated
xmlhttp.timeout = 500;
xmlhttp.ontimeout = function () {
content[i] = "none";
console.log("ontimeout: content[" + i + "] is now set to " + content[i] );
};
// On error
xmlhttp.onerror = function () {
content[i] = "none";
console.log("onerror: content[" + i + "] is now set to " + content[i] );
};
// On success
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
content[i] = xmlhttp.responseText;
// Deleting unneccessary spaces
content[i] = content[i].trim();
} else content[i] = "none";
}
};
//xmlhttp.setRequestHeader("Content-type","Lookup/simple");
xmlhttp.send();
}
我可以确认每个请求都会被发送。然而,google-chrome将它们标记为待定,而当diggin更深时,它将它们定义为停滞不前。
顺便说一句,此代码用于chrome扩展。因此,它可能是一个许可问题。虽然我不希望它是一个,因为我已经授予我的扩展来获取每个站点。 相应的清单文件包括以下行:
"permissions": [
"storage",
"http://*/",
"https://*/"
],