jquery - Ajax 进度条,直到我的成功功能完成
问题描述
我正在实现一个 ajax 进度条,我希望在我的 ajax 成功功能完成之前进度不应该达到 100%。
$.ajax({
data: //data,
type: 'post',
url: url,
xhr: function() {
var xhr = $.ajaxSettings.xhr();
xhr.upload.onprogress = function(e) {
var elem = document.getElementById("myBar");
var width = 10;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
}
else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';
}
}
return xhr;
},
success: // code
};
我的 ajax 进度条显示 100% ,但仍然等到成功功能完成。所以,我正在寻找我的进度条不应该达到 100%,直到我的成功功能被执行。
解决方案
像下面这样尝试,所以只有在完成后才会 100%。
html
<progress></progress>
jQuery 代码
$.ajax({
url: url,
type: 'POST',
enctype: 'multipart/form-data',
//Form data
data: new FormData($('#fileupload-form')[0]), // Data sent to server, a set of key/value pairs representing form fields and values
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false, // To unable request pages to be cached
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
processData: false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
//Custom XMLHttpRequest
xhr: function(){
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
$('progress').attr({
value: e.loaded,
max: e.total,
});
}
} , false);
}
return myXhr;
},
success: function(res){
}
});
推荐阅读
- python - 使用更新的元数据标签创建一个 geotiff 文件,而不使用 aux.xml 文件
- javascript - 如何在rails项目中验证文件类型
- python-2.7 - 反向shell端口转发
- php - 解析错误:语法错误,意外的 '(',在第 3 行的 C:\wamp\www\NewFirebase\Firebase.php 中期待 '{'
- reactjs - 有没有办法从回调函数 onPress 直接更改 Button 样式
- javascript - React 函数说“不是函数”
- greenplum - Greenplum 的 pg_dump 不支持 lock 语句
- ruby - 带有 ruby 和 watir 中的 URL 的整页屏幕截图
- android - 可观察延迟忽略设备动画设置
- android - 列表项到列表视图