js 中 ajax 同步 导致js 执行顺序 错乱


错误情景再现:

    yes: function (index, layero) {
                $('#update_staus').show();
                selectedDate = $('input[name=month]').eq(1).val();
                layer.close(index);
                
                
                update_status(selectedDate);

在上面的代码中  会先执行   update_status(selectedDate);。  然后执行

    $('#update_staus').show();

                selectedDate = $('input[name=month]').eq(1).val();

                layer.close(index)。 

               

    function update_status(selectedDate){
          
        var save_purchase_ret;
        var htmlobj = '<li class="">更新数据到hk明细表:<span id="save_purchase">更新中...</span></li>';
        $('#update_staus').append(htmlobj );
        var  status =  save_purchase_data(selectedDate);
        if(status === 200){

            继续执行 发送ajax 同步 请求  
}else{
    错误提示! 

}
      
        
    }

    function save_purchase_data(selectedDate) {
           var data;
        // 执行更新账单的操作,例如发送 AJAX 请求
        $.ajax({
            url: 'xx.com' + selectedDate,
            method: 'POST',
            async: false,
            success: function (res) {
                // 处理成功响应
                re = JSON.parse(res); 
                data = res ; //将ajax请求的结果 赋值给 data
   
                }
            },
            error: function (err) {
                // 处理错误
                console.log(err);
            
            }
        });
    }

一、修改的办法:

将所有的 添加标签  弄到ajax success 中去  。  并且将 ajax 重新设计为异步。

 $('#updateBillBtn').on('click', function () {
        
  
        var selectedDate;
        layer.open({
            title: '更新账单!',
            content: $('#datePickerContent').html(),
            area: ['300px', 'auto'],
            btn: ['确定', '取消'],
             
            yes: function (index, layero) {
                $('#update_staus').show();
                selectedDate = $('input[name=month]').eq(1).val();
                layer.close(index);
                
                
                update_status(selectedDate);
           
            }
        })

    });
  function update_status(selectedDate){
          
        var save_purchase_ret;
        var htmlobj = '<li class="">更新数据到hk明细表:<span id="save_purchase">更新中...</span></li>';
        $('#update_staus').append(htmlobj );
        save_purchase_data(selectedDate);
      
        
    }

    function save_purchase_data(selectedDate) {
           
        // 执行更新账单的操作,例如发送 AJAX 请求
        $.ajax({
            url: 'http://xx.com' + selectedDate,
            method: 'POST',
            // async: false,
            success: function (res) {
                // 处理成功响应
                re = JSON.parse(res); 
                if(re.code === 200) {
                    $('#save_purchase').text('更新完成');
                    var htmlobj = '<li class="layui">更新hk明细表:<span id="save_local_purchase">更新中...</span></li>';
                    $('#update_staus').append(htmlobj);
                    save_local_purchase_data(selectedDate);
                }
            },
            error: function (err) {
                // 处理错误
                console.log(err);
            
            }
        });
    }


总结

目前还不知道是什么原因造成的, 猜想 是 ajax  异步 同步的请求。

参考链接:Jquery ajax 同步阻塞引起的UI线程阻塞的坑(loading图片显示不出来,layer.load延迟) - 爱码网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值