我正在运行一个 js 函数,它使用 ajax 来查询 ldap 服务器以检索多个用户信息并输入到 mysql db。这是通过每次循环使用不同参数的 ldap 请求来完成的。
因为 ldap 服务器响应缓慢,所以我在进度条中添加,每次查询完成时,都会触发对进度条的更新。问题是进度条只在整个函数完成后更新一次,即使 css 更新发生在循环开始之前。
function findUsers() {
//set Number of users
var noOfUsers = 10;
var progress = 1;
//progress bar div appears as soon as function begins
//this element change doesn't happen till the function has finsihed
document.getElementById('progress_bar').style.visibility = 'visible';
//loop through users
for( counter = 0; counter <= noOfUsers; counter++)
{
//update element with progress
document.getElementById('progress_bar').innerHTML = progress + " of " + noOfUsers;
//start AJAX
xhrPostUserIDToLDAP =new XMLHttpRequest();
xhrPostUserIDToLDAP.onreadystatechange=function()
{
if (xhrPostUserIDToLDAP.readyState==4 && xhrPostUserIDToLDAP.status==200)
{
//updates progress var so on next loop, the element is updated
progress++;
}
}
xhrPostEmployees.open("POST","ajax/ldap.php",false);
xhrPostEmployees.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhrPostEmployees.send("userID="+userID);
}
//loop has finsihed, update element with complete notification
document.getElementById('progress_bar').innerHTML = "Completed"; }
唯一更新元素的是 Completed,但是一旦函数完成,这会在 20 秒后发生。如何在循环循环时更新元素?
谢谢