jQuery用done更新值,失败
jQuery to update values with done, fail
jQuery更新done, fail
Javascript, jQuery异步和作用域的东西让我抓狂。
输出是:
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;
}()
)
}],
问题是console.log("3rd:"~),
首先运行,所以它没有更新。我应该怎么做才能返回HighChart中GET
的值
您的匿名函数在任何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;
}())
}] //...
});
http://jsfiddle.net/2s2bf69c/3/相关文章:
- 解析云代码部署失败,并显示消息“更新失败,出现内部错误”
- 为什么当媒体查询更新 CSS 规则时,修改 CSS 规则会失败
- 更新文档,但出现错误:对于未定义的值,强制转换为字符串失败
- 更新失败,无法加载触发器.错误为“未捕获” 您必须使用 Parse.initialize 指定密钥
- JQuery AJAX调用本地更新脚本在Safari中失败,在Chrome和FF中工作
- jquery多选列表更新失败
- 更新到sql失败
- Rails模式表单-在表单提交失败后,用html.erb文件中的内容更新弹出窗口
- 在所有跨度上循环并更新一个失败的whit JQuery
- 从淘汰2.3更新到3.2时,嵌套模板失败
- 由于斜杠字符,Firebase多路径更新失败
- jQuery用done更新值,失败
- 付款处理成功,但数据库更新失败
- 对象动画-位置更新失败
- Firefox失败-使用文档后.写入和更新位置.哈希导致页面刷新
- 创建新对象失败,错误代码:没有找到用于更新/删除的对象
- jQuery -数据库访问失败后的更新按钮图标
- 我是如何失败的更新状态变量在ReactJS
- AngularJS函数更新json字符串值失败
- update'执行失败on 'ApplicationCache':没有应用程序缓存要更新