对不起。我还在学习令人惊叹的JQuery语言。我遇到了一个问题,阅读了很多,但仍然一团糟。希望你能指导我完成解决方案。
我有三个执行三次调用的函数。它们返回一个text var,我最近将其解析为JSON(如果可用)。
<script>
$(document).ready(function() {
...
afterAction:
a(param1,param2);
...
param1: [other value];
...
b(param1);
select:
a(param1,param2);
b(param1);
...
});
function a (param1,param2) {
// get data from DOM objects, val, texts, whatever
$.post ( "a.php", {
param1: param1,
param2: param2,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
}
});
function b (param1) {
// get data from DOM objects, val, texts, whatever
$.post ( "b.php", {
param1: param1,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
}
});
function c (param1) {
// get data from DOM objects, val, texts, whatever
$.post ( "c.php", {
param1: param1,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
}
});
</script>
事情是,所有这些功能都运作良好......当然,它们是异步的。我需要:
我尝试过但没有效果的事情:
非常感谢你能帮助我...
部分解决方案?努力吧!将$ .post更改为$ .ajax({... type:'POST'})并使用“return $ .ajax”和成功回调检索数据。后来我需要$ .when(a(param1,param2),b(param1 *),c(param1))。done(function(data1,data2,data3)){...});我可以处理单独数据集的声明。问题是,当param1是a和b函数的不同值时,先前要计算,
答案 0 :(得分:0)
快速的方法是将它们链接起来,在之前要执行的回调函数中调用它们中的每一个。
例如,由于a
是您的第一个函数,因此调用它,然后在其回调中根据需要操作变量并调用b
,并在其中执行相同操作以调用c
}。
基于您的代码的粗略示例:
$(document).ready(function() {
//call function a first
a(param1,param2);
});
function a (param1,param2) {
// get data from DOM objects, val, texts, whatever
$.post ( "a.php", {
param1: param1,
param2: param2,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
//change the state of param1 to whatever is required and then call function b
b(param1);
}
});
function b (param1) {
// get data from DOM objects, val, texts, whatever
$.post ( "b.php", {
param1: param1,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
//change the state of param1 to whatever is required and then call function c
c(param1);
}
});
function c (param1) {
// get data from DOM objects, val, texts, whatever
$.post ( "c.php", {
param1: param1,
stuff1: [data from DOM objects...]
},function(data){
console.log(data)
// do something (change dom objects properties, append..)
}
});