我希望我的输出符合这样的要求:
console.log("1st 1:", y1, y2, y3, y4);
console.log("1st 2:", y1, y2, y3, y4);
console.log("2nd 1:", y1, y2, y3, y4);
console.log("2nd 2:", y1, y2, y3, y4);
console.log("3rd:", y1, y2, y3, y4);但是我在Uncaught TypeError: undefined is not a function语句中得到了done。
只能看到:
1st 1: 5 5 5 5
1st 2: 8 30 236 365我找不到这段代码有什么问题:
data: (
function() {
// Test
y1 = 5,
y2 = 5,
y3 = 5,
y4 = 5;
// Ajax is asynchronous
function doRun() {
$.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);
}
});
return doRun;
};
doRun().done(function() {
console.log("2nd 1", y1, y2, y3, y4);
}).fail(function() {
console.log("2nd 2");
});
var data = [],
time = (new Date()).getTime(),
i;
for (i = -10; i <= 0; i++) {
console.log("3rd:", y1, y2, y3, y4);
data.push({
x: time + i * 10,
y: 0
});
}
return data;
}()
)我该怎么做才能解决这个问题,把所有的东西都打印好?
发布于 2014-08-21 22:53:06
function doRun() {
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);
}
});
};你把东西放错了。您返回了doRun --与调用的函数相同。doRun没有done属性。您的意思是从$.ajax中返回承诺。
发布于 2014-08-21 22:57:43
对doRun()的调用将返回doRun函数对象。它是否实际上定义了一个on ()方法?我猜不是。
https://stackoverflow.com/questions/25437147
复制相似问题