javascript - 进行多个同步 AJAX 调用并显示状态
问题描述
我有一个 for 循环,在其中我使用键进行 AJAX 调用并将结果附加到表中。每次通话后,我都会触发一个进度条。当我进行异步调用时,它会在 5-6 次调用后冻结并且没有响应。如果我使呼叫同步,它会完美运行,但这次我无法在进度条上显示状态,因为它会冻结页面。
$("#btn-fetch").on("click", function() {
var starts = $('#starts').val();
var ends = $('#ends').val();
var howmany = ends - start + 1;
$('#progress-text').html('<strong>%0</strong>');
$('#progress-status').css('width', '0%');
var percent = 100 / howmany;
var progress_status = 0;
for (var i = start; i <= ends; i++) {
$.ajax({
url: 'fetch.php',
data: {
id: i
},
type: "POST",
//async: false,
success: function(data) {
console.log(data);
$("#table-body").append(data);
progress_status += percent;
$('#progress-text').html('<strong>%' + progress_status.toFixed(2) + '</strong>');
$('#progress-status').css('width', progress_status.toFixed(2) + '%'); -
},
error: function(err) {
console.log(err) // Reject the promise and go to catch()
}
});
}
})
解决方案
推荐阅读
- mysql - 无法使用 MySql(Xampp) System.InvalidOperationException 更新我的 DataGridView:“连接已打开。”
- go - Retract Go 功能 - 将较旧的包版本撤回为不安全、不兼容或损坏
- sccm - Configuration Manager 正在寻找策略
- docker - 从 dockerhub 拉取镜像时需要 Docker Authentication 错误
- vue.js - Vue 组件之类的 gitlab 搜索通过
- swift - 如何在按下按钮时滚动到不同的页面,Xcode中的分页滚动视图
- c# - Realm Db 中是否存在类似于 NotMapped 的属性?
- javascript - 在 if 语句中检查一个项目是否存在于数组中的最易读和最有效的方法,即使项目为 0
- python - 返回 x 个月前的第一天和最后一天的 Python 函数
- python-2.7 - 如何在 PyQt4 的 QDateTimeEdit 中设置今天的日期和具体时间?