gpt4 book ai didi

javascript - 使用覆盖显示进度条的问题

转载 作者:行者123 更新时间:2023-11-30 18:56:22 25 4
gpt4 key购买 nike

需求 :我有一段代码类似于下面的代码。单击保存按钮后,我想在叠加层中显示进度条。一旦我收到来自 ajax 调用的响应,我就会显示一个警告,显示操作成功。

问题:覆盖(进度条)仅在我们完成后可见得到来自 ajax 调用的响应,我们显示警报。在处理 ajax 调用时它是不可见的。

我是在做错什么还是这是预期的行为?我正在使用纯 javascript。我不能使用任何外部库来执行此操作。

<script>
function save(){
document.getElementById('overlaydiv').style.display = 'block';
document.getElementById('progressBarDiv ').style.display = 'block';
var URL = 'some url';
ajaxFunction(URL, false);
}
function ajaxFunction(URL, isAsynchronousCall){
var xmlHttp;
var callTypeFlag = true; // is an Asynchronous Call
if(isAsynchronousCall != null){
callTypeFlag = isAsynchronousCall;
}
try{
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
xmlHttp.open("GET", URL, callTypeFlag);
xmlHttp.onreadystatechange = function(){responseProcessor(xmlHttp)};
xmlHttp.send(null);
}
function responseProcessor(xmlHttp){
//If the readystate is 4 then check for the request status.
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
alert('Settings saved successfully');
window.close();
opener.location.href='new URL';
}
}
}
</script>

<div id="overlaydiv" style="display:none"/>
<div id="progressBarDiv" style="display:none">
<img border="0" src="progressBar.gif"/>
</div>
<div>
<table>
<tr>
<td>
<input type="button" onclick="save()"/>
</td>
</tr>
</table>
</div>

最佳答案

在调用 ajaxFunction() 时稍微延迟

function save(){
document.getElementById('overlaydiv').style.display = 'block';
document.getElementById('progressBarDiv ').style.display = 'block';

setTimeout(ajaxFunction, 50);//5,10,20,30,40 will do
}

关于javascript - 使用覆盖显示进度条的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1938473/

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