问题描述
我正在尝试向我的服务器发送 HTTP 请求,直到请求通过。我当前的实现每秒发送太多请求,这会导致浏览器滞后。我试图每 2 秒发出一次请求。我的代码有问题还是我的实现有问题?
如果有更好的方法(除了多次发送请求),请随意填写
const GetWeightXML = () => {
var xmlhttp = new XMLHttpRequest();
var interval_1;
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
if (inverval_1 != null) {
clearInterval(interval_1);
}
var interval = setInterval(GetWeightXML_Recieved(this,interval),1000);
}
else if (this.status == 404) {
interval_1 = setInterval(GetWeightXML,2000);
}
}
let request_url = `${window.location.protocol}//${window.location.hostname}:${window.location.port}/Temp/weight.xml`;
xmlhttp.open("GET",request_url,true);
xmlhttp.send();
}
function GetWeightXML_Recieved(xml,interval) {
var xmlDoc = xml.responseXML;
let finished = xmlDoc.getElementsByTagName('Finished').childNodes[0].nodeValue;
if (finished == null || finished == "") {
return;
}
if (finished == "true") {
clearInterval(inverval);
return;
}
let currWeight = document.getElementsByClassName('current-weight')[0];
let timeElapsed = document.getElementsByClassName('time-elapsed')[0];
currWeight.innerText = "Current Weight: " + xmlDoc.getElementsByTagName('Weight').childNodes[0].nodeValue;
timeElapsed.innerText = "Time Elapsed: " + xmlDoc.getElementsByTagName('Time').childNodes[0].nodeValue;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)