简体   繁体   English

jQuery更新完成值,失败

[英]jQuery to update values with done, fail

jQuery to update values with done, fail jQuery更新完成值,失败

The Javascript, jQuery asynchronous and scope things are driving me crazy. Javascript,jQuery异步和作用域使我发疯。

Output is: 输出为:

3rd: 5 5 5 5 
1st 1: 5 5 5 5
1st 2: 9 36 284 340
2nd 9 36 284 340 
1st 1: 9 36 284 340
1st 2: 10 37 284 340
2nd 10 37 284 340 
...

All I want is to make the output to be: 我想要做的就是使输出为:

1st 2: 10 37 284 340
2nd 10 37 284 340 
3rd: 10 37 284 340
...

so that I can pass the updated values: 这样我就可以传递更新的值:

Code is as follows: 代码如下:

series: [{
    data: (
        function() {

            y1 = 5,
            y2 = 5,
            y3 = 5,
            y4 = 5;

            function myFunction() {
                return $.ajax({
                    type: "GET",
                    url: "/getTest",
                    success: function(data) {
                        console.log("1st 1:", y1, y2, y3, y4);
                        y1 = data.V1;
                        y2 = data.V2;
                        y3 = data.V3;
                        y4 = data.V4;
                        console.log("1st 2:", y1, y2, y3, y4);
                    }
                });
            };

            var data = [],
                time = (new Date()).getTime(),
                i;
            for (i = -60; i <= 0; i++) {
                myFunction().done(function() {
                    console.log("2nd", y1, y2, y3, y4);
                }).fail(function() {
                    console.log("Fail");
                });
                console.log("3rd:", y1, y2, y3, y4);
                data.push({
                    x: time + i * 30,
                    y: 0
                });
            }
            return data;
        }()
    )
}],

The problem is console.log("3rd:"~), runs first so it is not being updated. 问题是console.log("3rd:"~),它先运行,所以没有被更新。 What should I do to return the values from GET in HighChart. 我应该怎么做才能从HighChart中的GET返回值。

Your anonymous function is returning the array before any of your AJAX calls return. 您的匿名函数将在您的任何AJAX调用返回之前返回数组。 You need to set up your chart, then make your data calls and set the value of your series when they return. 您需要设置图表,然后进行数据调用并在序列返回时设置序列的值。 Something like: 就像是:

var chart = $('#container').highcharts({
    series: [{
        data: (

        function () {

            //We'll initialize an array of zeroes to set your series up
            var initialData = [];
                //This will eventually be populated with our data
            var ajaxData = [];
            //References to all our requests we'll make
            var ajaxRequests = [];

            y1 = 5,
            y2 = 5,
            y3 = 5,
            y4 = 5;

            function myFunction(x) {
                return $.ajax({
                    type: "GET",
                    url: "/getTest",
                    success: function (data) {
                        console.log("1st 1:", y1, y2, y3, y4);
                        y1 = data.V1;
                        y2 = data.V2;
                        y3 = data.V3;
                        y4 = data.V4;
                        console.log("1st 2:", y1, y2, y3, y4);
                        //Add the returned data to our array in the correct position
                        ajaxData.push({
                            x: x,
                            y: y1
                        });
                    }
                });
            };

            var time = (new Date()).getTime(),
                i;

            for (i = -60; i <= 0; i++) {
                //Push this initial node
                var x = time + i * 30;
                initialData.push({
                    x: x,
                    y: 0
                });
                //Fire off our ajax request, passing in the x value
                ajaxRequests.push(myFunction(x));
            }

            //Create a "master" request that will only be complete when all the ajax is done
            //When all requests are done, load the data into the chart
            //"apply" passes our array as arguments, i.e. function.apply($, [1, 2, 3]) === $.function(1,2,3)
            $.when.apply($, ajaxRequests).then(function() {
                console.log('Done getting ' + ajaxData.length + ' points');
                //Update the data of the first series in the chart
                chart.series[0].setData(ajaxData);
            });

            //Remember, this is dummy data...
            return initialData;
        }())
    }] //...
});

http://jsfiddle.net/2s2bf69c/3/ http://jsfiddle.net/2s2bf69c/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM