1

下面是我的代码,首先我使用 JSON 从数据库中获取数据,之后我将最初的 15 行绑定到图表,并且在 setInterval 函数中,我在 1 秒的间隔后不断绑定 1 行,我的问题是没有警报,即警报(“嗨“)我没有得到初步结果,我怎样才能在没有警报的情况下得到结果?

$(function () {
      $(document).ready(function () {
                var Data = "";
                var dataarray = [];
                var IdArray = [];
                var counter = 0;
                var chart;
                $('#container').highcharts({
                    chart: {
                        type: 'spline',
                        animation: Highcharts.svg, // don't animate in old IE
                        marginRight: 10,
                        events: {
                            load: function () {

                                var series = this.series[0];
                                setInterval(function () {
                                    var i = 16 + counter;
                                    var x = IdArray[i], // current time
                                     y = dataarray[i];
                                    series.addPoint([x, y], true, true);
                                    counter = counter + 1;
                                }, 1000);
                            }
                        }
                    },
                    title: {`enter code here`
                        text: 'Live HighChart From Database'
                    },
                    xAxis: {
                        type: 'decimal'

                    },
                    yAxis: {

                        title: {
                            text: 'Value'
                        }

                    },

                    series: [{
                        name: 'Data from database',
                        data: (function () {
                            // generate an array of random data

                            $.ajax({
                                type: 'POST',
                                dataType: 'json',
                                contentType: 'application/json',
                                url: 'LiveHighchart.aspx/GetData',  
                                data: '{}',
                                success:
                    function (response) {
                        Data = response.d;
                        for (var i = 0; i < Data.length; i++) {
                            dataarray[i] = Data[i].random;
                            IdArray[i] = Data[i].Id;
                        }
                    }
                            });
                            var data = [];
                            alert("hi");

                            for (var i = 0; i < 15; i++) {
                                data.push({
                                    x: IdArray[i],
                                    y: dataarray[i]
                                });
                            }

                            return data;

                        })()
                    }]
                });
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto">
        </div>
    </div>
    </form>
</body>
</html>
4

2 回答 2

0

我认为这是因为查询尚未返回结果 - 警报导致您的应用暂停,这意味着结果可以及时传递。您应该考虑使用回调 - 仅在返回 AJAX 调用的数据时运行的函数。

于 2013-11-13T14:16:50.297 回答
0

I Solved it myself ,code after ajax request was executing before ajax request loads data: this line (async: false) in ajax request forced code after ajax to pause until ajax loads data

于 2013-11-14T09:49:04.517 回答