【问题标题】:jQuery Ajax Progress - how to not inherit progress percentage on different ajax requests?jQuery Ajax Progress - 如何不继承不同 ajax 请求的进度百分比?
【发布时间】:2013-07-24 04:35:17
【问题描述】:

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

它只是在 ajax 请求中添加了一个 progress: 语句。

效果很好,我的代码如下所示:

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 将被共享 两个请求,所以如果 request 1 处于 40% progress (pct) 那么 request 2 将从request 1

继承相同的进度百分比(pct = 40%)

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

【问题讨论】:

  • 问题是which有相同的值或者两组ajax进度调用。如果你得到which info 一个闭包对于每个调用都是唯一的,它应该可以工作。最后打印出来的file_name 和最后的进度更新也会遇到同样的问题。
  • @LeeMeador _which 在每个change() 上更改他的值,所以在随后启动的每个请求中,但我不能这样做 var _which.pct !?
  • @LeeMeador 不,伙计,每个请求的_file_name 总是新的,检查更好我已经测试过并且它有效,只是问题是使 pct 不共享:P
  • @LeeMeador 对不起我的错误我粘贴了整个 js 现在很清楚我认为..

标签: javascript jquery progress percentage


【解决方案1】:

这将存储_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();

【讨论】:

  • 所以我需要将 ajax 放入带参数的方法中?这不会分享进度:声明吗?我是新手,如果您可以添加一些关于它为什么起作用的描述,那就太棒了! ;) @Lee Meador
猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多