上次的AJAX定时刷新多ID不正确,这次请教了高手之后补全

关键是setInterval无法传递参数,所以用了匿名函数再包裹一下就好了。

 

 

复制代码
//重置发布进度
    function resetPercent(id_data){
        $.ajax({
              url:'/autodeploy/reset_percent/' + id_data,
              success: function(json){
                },
            });
    };
    //间隔刷新,显示百分比,AJAX调用rest_framework框架
    var _interval = {};

    function showPercent(id_data){
        //var _interval;
        var percent_value;
        var id_subserver = id_data.split("-")[0]

        $.getJSON("/api/subserver/" + id_subserver,
            function(data,state){
                if (state == 'success') {
                    percent_value_array = data.deploy_status.split(",");
                    percent_value = percent_value_array[0];
                    cmd_value = percent_value_array[1];
                    $('#' + id_data).html("<div class='uk-progress uk-progress-striped uk-progress-active'><div class='uk-progress-bar' style='width: " + percent_value + "%;'>" +  cmd_value + ' ' + percent_value + "%</div><i class='uk-icon-cog uk-icon-spin'></i></div>");
                     console.log(percent_value[0])
                        if ( percent_value != 100 ){
                            //_interval = setInterval(showPercent(id_data), 3000);
                        } else {
                             clearInterval(_interval[id_data]);
                             $('#' + id_data).html("<div class='uk-progress uk-progress-striped uk-progress-success'><div class='uk-progress-bar' style='width: " + percent_value + "%;'>100%</div></div>");
                        };

                    };
            }
        );
    };

    $(".btn-multi-deploy").click(function(){
          var deploypool_id =  $(this).attr("deploypool_id")
          var group_data = $("#deploy-form" + deploypool_id).serialize()
          var deploy_type = $(this).attr("deploy_type")
          var _self = this;

          console.log(deploypool_id, group_data)

          //因为涉及到模态对话态中的AJAX输出ID定位,所以用了两个数组来重置更新进度和刷新百分比。可以优化。
          var id_array = []
          var id_s_array = []
          var group_array = group_data.split("&");
          for (var key_data in group_array) {
            if (group_array[key_data].indexOf("select-deployversion") != -1) {
                deploy_id = group_array[key_data].split("=")[1]
            };
            if (group_array[key_data].indexOf("check-server") != -1) {
                id_s_array.push(group_array[key_data].split("=")[1])
                id_array.push(group_array[key_data].split("=")[1] + "-" +deploy_id)

            };
          };

          promiseDEPLOY = $.ajax({
            url:'{% url "autodeploy:group-cmd" %}',
            type: 'post',
            data:{
                group_data: group_data,
                deploy_type: deploy_type,
            },
            dataType: 'json',
            beforeSend: function(){
                result_output = $(_self).siblings("p.result_output");
                result_output_desc = $(_self).siblings("p.result_output_desc");
                $(_self).attr('disabled',"true");
                $(_self).append(" <i class='uk-icon-cog uk-icon-spin'></i>");
                result_output_desc.append(" <strong>集群发布中,请耐心等候....</strong>");
                // result_output.html("<div >集群发布中,请耐心等候....</div>");
                //发送前重置
        for( index=0;index < id_s_array.length;index++){
                    resetPercent(id_s_array[index]);
                }
            },
            success: function(json){
                $(_self).children('i').remove();
                $(_self).append(" <i class='uk-icon-check'></i>")
                result_output_desc.append("<span class='uk-text-success'><i class='uk-icon-check'></i></span>");
                result_output_desc.append("<span class='uk-text-success'>异步队列分发完成,每个服务器开始更新。</span>");
                //发送后获取进度
            
         for( index=0;index < id_array.length;index++){
            var key = id_array[index];
                    //_interval = setInterval(function(){showPercent(key)}, 3000);
            _interval[key] = setInterval(function(){showPercent(key);}, 3000);
                }
        },
            error: function(){
                result_output_desc.append("<span class='uk-text-muted'><br>服务器内部错误 </span>");
                result_output_desc.append("<span class='uk-text-danger'><i class='uk-icon-remove'></i></span>");
                $(_self).children('i').remove();
                result_output.html("<div ></div>");
            },
            complete: function(){
            }
        });/* end cmd-run stop */
    });
复制代码

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值