jQueryAjax进度-如何不继承不同Ajax请求的进度百分比

jQuery Ajax Progress - how to not inherit progress percentage on different ajax requests?

本文关键字:请求 Ajax 百分比 进度 何不 继承 jQueryAjax      更新时间:2023-09-26

嗨,我实际上在使用这个插件https://github.com/englercj/jquery-ajax-progress/

它只是向ajax请求添加了一个progress:statment。

它工作得很好,我的代码看起来像这样:

var _min_width = 470;
var _min_height = 330;
var _which;
var _fyle_type;
var _file_name;
var allowed_types = new Array('image/png','image/jpg','image/jpeg');
if (typeof(FileReader) === 'function'){
$('input[type="file"]').on('change', function(e) {
     _file_name = $(this).val();
    var file = e.target.files[0];
    if (!in_array(file.type,allowed_types) || file.length === 0){
        notify("You must select a valid image file!",false,false); 
        return;
    }
    if(file.size > 3145728 /*3MB*/){
        notify("<?php echo lang('each-photo-1MB'); ?>",false,false); 
        return;
    }
    notify_destroy();
    var reader = new FileReader();
    reader.onload = fileOnload;
  reader.readAsDataURL(file);

});
function fileOnload(e) {
    var img = document.createElement('img');
    img.src = e.target.result;
    img.addEventListener('load', function() {
        if(img.width < _min_width || img.height < _min_height ){
        notify("<?php echo lang('each-photo-1MB'); ?>",false,false); 
        return;
        }
       //remove not-needed base64 data:pfff
       var clear_string =  e.target.result.replace('data:image/jpeg;base64,','').replace('data:image/png;base64,','');
       var _data;
       if(_which == 'photo_1'){
        _data = {photo_1:clear_string};
       }if(_which == 'photo_2'){
        _data = {photo_2:clear_string};
       }if(_which == 'photo_3'){
        _data = {photo_3:clear_string};
       }if(_which == 'photo_4'){
        _data = {photo_4:clear_string};
       }
            $.ajax({
            type:'post',
            dataType:'text',
            data:_data,
            url:_config_base_url+'/upload/upload_photos',
            beforeSend:function(){
            $('.'+_which+'_holder').fadeOut(0);
            $('.'+_which+'_progress').fadeIn(0);
            $('.'+_which+'_progress').addClass('progress-striped');
            },  
            progress:function(e){
            //make sure we can compute the length
            if(e.lengthComputable) {
            //calculate the percentage loaded
            var pct = (e.loaded / e.total) * 100;
            $('.'+_which+'_progress .bar').css({'width':pct+'%'});
            $('.'+_which+'_progress .progress-opt span').text(pct.toFixed(0));
            //console.log(pct);
            }else {
            console.warn('Content Length not reported!');
            }
            },
            success:function(){
            alert(_file_name+' uploaded ok');
            },
            complete:function(){
            $('.'+_which+'_progress .progress-opt span').text('100');
            $('.'+_which+'_holder p').text(_file_name);
            $('.'+_which+'_progress .bar').css({'width':'100%'});
            $('.'+_which+'_progress').delay(300).removeClass('progress-striped').fadeOut(0,function(){
            $('.'+_which+'_holder').show(0);
            $('.'+_which+'_progress .bar').css({'width':'0'});
            });

            }
            });

    });
}
}

现在的问题是,当我在progress: ajax语句中设置text(pct.toFixed(0))时,我需要使pct"活"起来。我的意思是,这段代码以百分比表示请求进度,这样你就可以看到请求结束所需的百分比。

问题是,如果我启动其中2个请求,pct就会被共享两个请求,因此如果请求1的进度(pct)为40%,则的请求2请求1继承相同的进度百分比(pct=40%)


为了更好地解释,我想启动许多这样的请求,但要>保持每个请求的进度百分比(var-pct)是唯一的

这将存储_which的值,以便在调用处理程序时重用:

function(_which, file_name) {
    $.ajax(... same as the code you have ...
}(_which, file_name);

这样做的目的是创建一个闭包,其中包含您在传递给ajax调用的回调(处理程序)函数中使用的两个变量的值。通过这样做,您仍然可以共享函数(例如进度),但不会共享这些函数中使用的_whichfile_name的值。

当然,你会在整个呼叫中心$.ajax 周围添加第1行和第3行

如果添加更多在外部设置但在这些处理程序函数内部使用的变量,则还需要将它们添加到闭包中。

还有其他方法可以做到这一点,但这只是对代码的最小更改。

例如,这也可以:

var doAjax = function() {
    var localWhich = _which;
    var localFileName = file_name;
    $.ajax(... all the same code but use localWhich where you have _which now
         ... and localFileName where you have file_name now
}
doAjax();