Javascript 使用进度条跟踪多个jquery ajax API请求
我有一个增量,它获取ID并将它们添加到url中,以发出多个ajax请求。Javascript 使用进度条跟踪多个jquery ajax API请求,javascript,jquery,ajax,twitter-bootstrap,progress-bar,Javascript,Jquery,Ajax,Twitter Bootstrap,Progress Bar,我有一个增量,它获取ID并将它们添加到url中,以发出多个ajax请求。 js: for (x = 1; x <= val['id_count']; x++) { $.ajax({ url: baseUrl + x, dataType: "json", success: function(results, status, xhr) { }, error: function(xhr, status, error) { $(&
js:
for (x = 1; x <= val['id_count']; x++) {
$.ajax({
url: baseUrl + x,
dataType: "json",
success: function(results, status, xhr) {
},
error: function(xhr, status, error) {
$("#message").html("data: " + status + " " + error)
}
})
}
for (x = 1; x <= val['id_count']; x++) {
totalIDs = 0;
totalIDs = x.length;
$.ajax({
url: baseUrl + x,
dataType: "json",
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.onreadystatechange = function() {
var progress = x / totalIDs * 100;
$(".progress-bar").css({
"width": progress + "%"
});
}
return xhr;
},
success: function(results, status, xhr) {
},
error: function(xhr, status, error) {
$("#message").html("data: " + status + " " + error)
}
})
}
for(x=1;xfor(x=1;x@rorymcrossan)有限制吗?它看起来很疯狂,但是api没有一次给出所有信息,所以我必须像这样循环它们,而且它有很多请求,需要一些时间,所以我需要看看后台发生了什么。看看-你可以使用.ajaxSend()来增加“进行中"和。ajaxComplete到减量it@freedomn-m谢谢,伙计,我确实读过这些东西,但我是新手,所以我需要更多的解释。看起来你可以将进度更新移到success:
,而不是xhr:
-xhr:
有点高级,你不太可能需要使用它。你可以将var-inprogress
然后inprogress++
作为for
和inprogress--
的success
的inprogress>的var-progress=(totalIDs-inprogress)/totalIDs*100
的第一行,不幸的是,它不起作用了。或者我犯了一个错误idk
for (x = 1; x <= val['id_count']; x++) {
totalIDs = 0;
totalIDs = x.length;
$.ajax({
url: baseUrl + x,
dataType: "json",
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.onreadystatechange = function() {
var progress = x / totalIDs * 100;
$(".progress-bar").css({
"width": progress + "%"
});
}
return xhr;
},
success: function(results, status, xhr) {
},
error: function(xhr, status, error) {
$("#message").html("data: " + status + " " + error)
}
})
}
for (x = 1; x <= val['id_count']; x++) {
$.ajax({
url: baseUrl + x,
dataType: "json",
success: function(results, status, xhr) {
if (inprogress < total_urls) {
inprogress++;
$("#tProgress").html(inprogress + ' / ' + total_urls + ' ✓');
progress = Math.round(inprogress / total_urls * 100);
$("#progress-bar").css({
"width": progress + "%"
});
$("#progress-bar").html(progress + '%');
}
},
error: function(xhr, status, error) {
$("#message").html("data: " + status + " " + error)
}
})
}