Javascript 当xhttp对象从XML获取数据时,如何显示进度计数器?
我有一个带有脚本的HTML页面,其中一个xhttp对象从XML文件中读取节点值并将其写入。在读取所有必需的节点之前,我希望以百分比Javascript 当xhttp对象从XML获取数据时,如何显示进度计数器?,javascript,xml,html,xmlhttprequest,Javascript,Xml,Html,Xmlhttprequest,我有一个带有脚本的HTML页面,其中一个xhttp对象从XML文件中读取节点值并将其写入。在读取所有必需的节点之前,我希望以百分比(read_rows/total_rows*100)显示一个数字进度指示器。但是由于整个事情都是在一个线程上运行的,所以我无法实现这一点。对我该做什么有什么建议吗 下面是一些javascript代码- function fetch(xml_file, index) { //create xhttp object, read xml, get nodeValu
(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>
获取(数据文件、索引);索引++
获取(数据文件、索引);索引++
获取(数据文件、索引);索引++
当每个
都被填充时,我想在某个地方显示进度,比如一个覆盖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
}
通过使用回调函数,我可以在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
}
您可以传递一个回调函数来获取并使用它来报告进度。或者使请求异步并侦听进度事件。但是,您必须处理可能不完整的元素…您的代码将无法工作,xhttp.send是一种异步方法,您需要使用
onreadystatechange
回调函数才能正常工作。谢谢。您可以传递回调函数来获取并使用它来报告进度。或者使请求异步并侦听进度事件。但是,您必须处理可能不完整的元素…您的代码将无法工作,xhttp.send是一种异步方法,您需要使用onreadystatechange
回调函数才能正常工作。谢谢