【BUG解决方案】jQuery数组中包含数据,但通过 .length 获得的数组长度始终为0

0. BUG展示

var lels = [];
for (var i = 0; i < maxDevNums + 1; i++) {
    lels.push([]);
}
$.ajax({
    type : "post",
    async : true,
    url : "/sc/comb/history/data",
    data : {},
    dataType : "json",
    success : function (result) {
        if (result) {
            for (let i = 0; i < result.length; i++) {
                lels[result[i].combDevId].push(result[i].combLel);
            }
            console.log(lels);
        }
        console.log("在ajax请求中获取数组长度:" + lels[1].length)
    },
    error : function(errorMsg) {
        alert("图表请求数据失败");
    }
})

console.log("在ajax请求外获取数组长度:" + lels[1].length);

for (let i = 1; i < lels.length; i++) {
    var gaugeChart = echarts.init(document.getElementById("echarts-gauge-chart" + i));
    if (lels[i].length > 0) {
        var lel = lels[i][lels[i].length - 1];
        gaugeOption.series[0].data = [{value: lel, name: 'lel'}]
    } else {
        gaugeOption.series[0].data = [{value: 0, name: 'offline'}]
    }
    gaugeChart.setOption(gaugeOption);
    $(window).resize(gaugeChart.resize);
}

在这里插入图片描述
在这里插入图片描述

1. 原因分析

ajax默认是异步(async : true),代码第25行打印数组lel长度时,接口数据未返回,所以数组lel长度为0

2. 解决方案

async : true修改为async : false,即异步改为同步。因为是同步请求,所以在打印时接口数据已返回,所以数组lel长度为其实际长度4。

var lels = [];
for (var i = 0; i < maxDevNums + 1; i++) {
    lels.push([]);
}
$.ajax({
    type : "post",
    async : false,
    url : "/sc/comb/history/data",
    data : {},
    dataType : "json",
    success : function (result) {
        if (result) {
            for (let i = 0; i < result.length; i++) {
                lels[result[i].combDevId].push(result[i].combLel);
            }
            console.log(lels);
        }
        console.log("在ajax请求中获取数组长度:" + lels[1].length)
    },
    error : function(errorMsg) {
        alert("图表请求数据失败");
    }
})

console.log("在ajax请求外获取数组长度:" + lels[1].length);

for (let i = 1; i < lels.length; i++) {
    var gaugeChart = echarts.init(document.getElementById("echarts-gauge-chart" + i));
    if (lels[i].length > 0) {
        var lel = lels[i][lels[i].length - 1];
        gaugeOption.series[0].data = [{value: lel, name: 'lel'}]
    } else {
        gaugeOption.series[0].data = [{value: 0, name: 'offline'}]
    }
    gaugeChart.setOption(gaugeOption);
    $(window).resize(gaugeChart.resize);
}

在这里插入图片描述
在这里插入图片描述

3. 附:页面完整代码

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('百度ECharts')" />
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备1</h5>
                    <!--<div class="ibox-tools">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        &lt;!&ndash;<ul class="dropdown-menu dropdown-user">
                            <li><a href="#">查看当前设备详细信息</a></li>
                            &lt;!&ndash;<li><a href="#">查看历史可燃气体浓度</a></li>&ndash;&gt;
                            <li><a href="javascript:void(0)" οnclick="$.modal.open('历史可燃气体浓度', '/sc/map/comb-history');">查看历史可燃气体浓度</a></li>
                        </ul>&ndash;&gt;
                    </div>-->
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart1"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备2</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart2"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备3</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart3"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备4</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart4"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备5</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart5"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备6</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart6"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备7</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart7"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备8</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart8"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备9</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart9"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备10</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart10"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备11</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart11"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>设备12</h5>
                </div>
                <div class="ibox-content">
                    <div class="echarts" id="echarts-gauge-chart12"></div>
                    <div align="right"><button type="button" class="btn btn-success btn-sm" href="javascript:void(0)" onclick="$.modal.open('历史可燃气体浓度', '/sc/comb/history');">历史可燃气体浓度与设备信息</button></div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: echarts-js" />
<script type="text/javascript">
    $(function () {
        var maxDevNums = 12;

        var gaugeData = [{value: 98, name: 'lel'}]

        var gaugeOption = {
            /*title : {
                text: '运行状态: '
            },*/
            tooltip : {
                formatter: "{a} <br/>{c} {b}"
            },
            /*toolbox: {
                show : true,
                feature : {
                    dataView : {show: true, readOnly: false},
                    mark : {show: true},
                    /!*restore : {show: true},
                    saveAsImage : {show: true}*!/
                }
            },*/
            series : [
                {
                    name:'浓度',
                    type:'gauge',
                    min:0,
                    max:100,
                    splitNumber:10,
                    axisLine: {            // 坐标轴线
                        lineStyle: {       // 属性lineStyle控制线条样式
                            width: 10
                        }
                    },
                    axisTick: {            // 坐标轴小标记
                        length :15,        // 属性length控制线长
                        lineStyle: {       // 属性lineStyle控制线条样式
                            color: 'auto'
                        }
                    },
                    splitLine: {           // 分隔线
                        length :20,        // 属性length控制线长
                        lineStyle: {       // 属性lineStyle(详见lineStyle)控制线条样式
                            color: 'auto'
                        }
                    },
                    title : {
                        textStyle: {       // 其余属性默认使用全局文本样式,详见TEXTSTYLE
                            fontWeight: 'bolder',
                            fontSize: 20,
                            fontStyle: 'italic'
                        }
                    },
                    detail : {
                        fontSize: 20,
                        formatter: '{value}%',
                        textStyle: {       // 其余属性默认使用全局文本样式,详见TEXTSTYLE
                            fontWeight: 'bolder'
                        }
                    },
                    data: gaugeData
                }
            ]
        };

        var lels = [];
        for (var i = 0; i < maxDevNums + 1; i++) {
            lels.push([]);
        }
        $.ajax({
            type : "post",
            async : false,
            url : "/sc/comb/history/data",
            data : {},
            dataType : "json",
            success : function (result) {
                if (result) {
                    for (let i = 0; i < result.length; i++) {
                        lels[result[i].combDevId].push(result[i].combLel);
                    }
                    console.log(lels);
                }
                console.log("在ajax请求中获取数组长度:" + lels[1].length)
            },
            error : function(errorMsg) {
                alert("图表请求数据失败");
            }
        })

        console.log("在ajax请求外获取数组长度:" + lels[1].length);

        for (let i = 1; i < lels.length; i++) {
            var gaugeChart = echarts.init(document.getElementById("echarts-gauge-chart" + i));
            if (lels[i].length > 0) {
                var lel = lels[i][lels[i].length - 1];
                gaugeOption.series[0].data = [{value: lel, name: 'lel'}]
            } else {
                gaugeOption.series[0].data = [{value: 0, name: 'offline'}]
            }
            gaugeChart.setOption(gaugeOption);
            $(window).resize(gaugeChart.resize);
        }
    });
</script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木何日可逢春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值