jQuery to update values with done, fail
The Javascript, jQuery asynchronous and scope things are driving me crazy.
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. What should I do to return the values from GET
in HighChart.