基本上,我正在为外部登录系统执行AJAX请求,如何根据请求的长度更新进度条?
例如,请求需要在 1.30s 到 1.40s 之间完成,如何根据特定时间间隔更新进度条,例如每次更新10% 10ms或者其他什么,这是进度条的HTML布局
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" aria-valuenow="65" aria-valuemin="0" aria-valuemax="100" style="width: 65%">
<span class="sr-only">65% Complete</span>
</div>
</div>
进度条的长度使用width: 65%
属性
这个想法基本上是让它看起来像是根据请求进行更新,所以当请求完成时,百分比栏就满了
答案 0 :(得分:29)
我认为这篇文章很清楚 http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/
发布此信息以供将来参考(如果删除博客):
$.ajax({
xhr: function(){
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
//Download progress
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){
//Do something success-ish
}
});
答案 1 :(得分:0)
您可以使用jquery形式plugin并使用此方法“ uploadProgress ”,如下所示:
$('#register-form').on('submit', function(e) {
e.preventDefault();
$(this).ajaxSubmit({
url: url,
uploadProgress: function (event, position, total, percentComplete){
$('.progress-bar').width(percentComplete + '%');
$('.progress-bar > p').html(percentComplete);
},
success: function (response, statusText, xhr, $form) {
// TODO: You'll need to do some custom logic here to handle a successful
// form post, and when the form is invalid with validation errors.
},
error: function(a, b, c) {
// NOTE: This callback is *not* called when the form is invalid.
// It is called when the browser is unable to initiate or complete the ajax submit.
// You will need to handle validation errors in the 'success' callback.
console.log(a, b, c);
}
});
});