我的代码适用于Chrome和Safari,但它在FF中挂起。
我删除了不必要的代码部分。
我使用控制台命令来显示第一个循环到达的距离,并且它将在xhr打开和发送命令之前完成第二个日志。
如果存在open / send命令,则循环仅发生一次,如果我删除open / send命令,则循环成功完成。
目前正在使用FF 62nightly,但是这个问题一直困扰着我,因为昆腾已经问世,我现在正试图弄清楚为什么它不能正常工作。
for (i = 0; i < length; i++) {
(function(i) {
// new XMLHttpRequest
xhr[i] = new XMLHttpRequest();
// gets machine url from href tag
url = rows[i].getElementsByTagName("td")[0].getElementsByTagName('a')[0].getAttribute('href');
// Insert the desired values at the end of each row;
// will try to make this customizable later as well
insertVNC[i] = rows[i].insertCell(-1);
insertSerial[i] = rows[i].insertCell(-1);
insertVersion[i] = rows[i].insertCell(-1);
insertFreeDiskSpace[i] = rows[i].insertCell(-1);
// the fun part: this function takes each url, loads it in the background,
// retrieves the values needed, and then discards the page once the function is complete;
// In theory you could add whatever you want without taking significantly longer
// as long as it's on this page
console.log(i);
xhr[i].onreadystatechange = function() {
if (xhr[i].readyState == 4 && xhr[i].status == 200) {
}
};
//"Get" the "Url"... true means asyncrhonous
console.log(url);
xhr[i].open("GET", url, true);
xhr[i].send(null);
})(i); //end for loop
}
答案 0 :(得分:0)
我无法告诉你为什么它会在Firefox中出现问题。我不相信会从任何浏览器发送任意多个请求
我会亲自尝试这个,因为它不会触发下一个直到一个完成
const urls = [...document.querySelectorAll("tr>td:nth-child(0) a")].map(x => x.href);
let cnt=0;
function getUrl() {
console.log(urls[cnt]);
xhr[i].open("GET", urls[cnt], true);
xhr[i].send(null);
}
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr[i].readyState == 4 && xhr[i].status == 200) {
if (cnt>urls.length) getUrl();
cnt++;
}
}
getUrl();