Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery使用done、fail更新值_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript jQuery使用done、fail更新值

Javascript jQuery使用done、fail更新值,javascript,jquery,ajax,Javascript,Jquery,Ajax,jQuery使用done、fail更新值 Javascript、jQuery异步和scope这些东西让我抓狂 输出为: 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 ... 我只想将输出设置为: 1st 2: 10 37 284 340 2nd 10 37 284 340 3rd: 10

jQuery使用done、fail更新值

Javascript、jQuery异步和scope这些东西让我抓狂

输出为:

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 
...
我只想将输出设置为:

1st 2: 10 37 284 340
2nd 10 37 284 340 
3rd: 10 37 284 340
...
以便我可以传递更新的值:

代码如下:

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;
        }()
    )
}],
系列:[{
数据:(
函数(){
y1=5,
y2=5,
y3=5,
y4=5;
函数myFunction(){
返回$.ajax({
键入:“获取”,
url:“/getTest”,
成功:功能(数据){
控制台日志(“第一个1:”,y1,y2,y3,y4);
y1=数据0.V1;
y2=data.V2;
y3=data.V3;
y4=数据0.V4;
控制台日志(“第1个2:”,y1,y2,y3,y4);
}
});
};
var数据=[],
时间=(新日期()).getTime(),
我

对于(i=-60;i您的匿名函数将在任何AJAX调用返回之前返回数组。您需要设置图表,然后进行数据调用,并在它们返回时设置序列值。类似于:

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;
        }())
    }] //...
});
var-chart=$(“#容器”).highcharts({
系列:[{
数据:(
函数(){
//我们将初始化一个零数组来设置序列
var initialData=[];
//这将最终填充我们的数据
var ajaxData=[];
//我们将提出的所有要求的参考资料
var ajaxRequests=[];
y1=5,
y2=5,
y3=5,
y4=5;
函数myFunction(x){
返回$.ajax({
键入:“获取”,
url:“/getTest”,
成功:功能(数据){
控制台日志(“第一个1:”,y1,y2,y3,y4);
y1=数据0.V1;
y2=data.V2;
y3=data.V3;
y4=数据0.V4;
控制台日志(“第1个2:”,y1,y2,y3,y4);
//将返回的数据添加到数组的正确位置
ajaxData.push({
x:x,
y:y1
});
}
});
};
变量时间=(新日期()).getTime(),
我

对于(i=-60;i我仍然需要在
initialData.push({
但是在
initialData.push.push里面,{
我无法访问
y1
y2
,…除非您想等到所有远程调用返回后再调用highcharts(),否则无法在initialData中包含它们-数据根本不可用,这就是为什么您必须等到它们全部完成,然后调用series.setData()。