Javascript 使用自定义进度事件和Ajax GC上传jQuery文件
我已经使用jQuery.ajax在一个应用程序中内置了一个带有进度条的文件上传 但我担心的是,我不明白垃圾收集将如何处理它,因此它的内存效率如何。 对于进度条,为了添加“进度”事件,我必须添加一个自定义xhr 何时清理自定义xhr函数和“进度”事件处理程序?Javascript 使用自定义进度事件和Ajax GC上传jQuery文件,javascript,jquery,ajax,garbage-collection,Javascript,Jquery,Ajax,Garbage Collection,我已经使用jQuery.ajax在一个应用程序中内置了一个带有进度条的文件上传 但我担心的是,我不明白垃圾收集将如何处理它,因此它的内存效率如何。 对于进度条,为了添加“进度”事件,我必须添加一个自定义xhr 何时清理自定义xhr函数和“进度”事件处理程序? myApp.prototype.uploadFile = function () { $.ajax({ type : "POST", url : posturl, x
myApp.prototype.uploadFile = function () {
$.ajax({
type : "POST",
url : posturl,
xhr : function () { // custom xhr
var myXhr = $.ajaxSettings.xhr();
this.onSaveProgress(myXhr, file);
return myXhr;
}.bind(this),
contentType: false,
processData: false,
data : postdata,
beforeSend : this.onBeforeSend,
success : this.onSaveSuccess,
error : this.onSaveError
});
},
myApp.prototype.onSaveProgress = function (xhr, file) {
if (!xhr.upload)
return;
xhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
var percent = Math.floor((e.loaded / e.total)*100) + "%";
$("#progress" + file.id).css("width", percent).text(percent);
}
}, false); // add new progress event handler
}
如您所见,我需要访问“progress”事件处理程序中的文件对象,以便更新正确的进度条