gpt4 book ai didi

javascript - 当 xhttp 对象从 XML 获取数据时,如何显示进度计数器?

转载 作者:行者123 更新时间:2023-11-28 02:25:47 25 4
gpt4 key购买 nike

我有一个带有脚本的 HTML 页面,其中 xhttp 对象从 XML 文件读取节点值并写入它们。在读取所有必需的节点之前,我想以百分比 (read_rows/total_rows * 100) 显示数字进度指示器。但由于整个事情都在一个线程上运行,我无法完成这一任务。对我应该做什么有什么建议吗?

这是一些 JavaScript 代码-

function fetch(xml_file, index) {

//create xhttp object, read xml, get nodeValue of element "VALUE" etc...

xhttp = new XMLHttpRequest();
xhttp.open("GET", xml_file ,false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
data = xmlDoc.getElementsByTagName("VALUE")[index].childNodes[0].nodeValue;
document.write(data);

// CALCULATE PROGRESS AND WRITE TO SOME OVERLAY DIV...

}

这是 HTML 的一部分-

<p><script>fetch(data_file, index); index++;</script></p>
<p><script>fetch(data_file, index); index++;</script></p>
<p><script>fetch(data_file, index); index++;</script></p>

正如每个<p></p>已满,我想在某处显示进度,例如覆盖 DIV。我如何同时做到这一点?

最佳答案

通过使用回调函数,我可以在 xhttp 对象从 XML 文件读取数据时显示进度。

我已将代码更改为 -

function fetch(xml_file, index) {

//create xhttp object, read xml, get nodeValue of element "VALUE" etc...

xhttp = new XMLHttpRequest();
xhttp.open("GET", xml_file ,false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
data = xmlDoc.getElementsByTagName("VALUE")[index].childNodes[0].nodeValue;
document.write(data);

progress(index); //CALLBACK FUNCTION

}

function progress(index) {
document.getElementById("ProgressString").innerHTML = Math.round((index/358)*100) + "% complete"; // 358 nodes were to be read
}

关于javascript - 当 xhttp 对象从 XML 获取数据时,如何显示进度计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com