假设我有三个功能作为这样:等待多个条件然后执行功能
function A() {
//does a jquery getJson() request and applies json data to html
function C()
}
function B() {
//does a jquery getJson() request and applies json data to html
function C()
}
function C(){
// applies cosmetic changes to the already set html which was made be Functions A & B.
//for example it converts numbers 1-5 set in a span to starred rating.
}
的问题是,功能A和B是异步和它们的回调执行依赖于截取JSON的接收时间。并因此调用函数C在json收到的一个或另一个数据集上失败。
所以我想要的是,应该在收到两个jsons并将其数据绘制到页面上之后执行ony函数C.它只能被调用一次。
我有一种感觉,这可能是封闭式的,但我不确定。
查找“拍拍承诺燕鸥”。你可以用jQuery轻松做到这一点。 – elclanrs
好的。我会看看。 – codin
因此,根据我得到的三个答案,他们都以某种方式推荐同步执行。我想知道如果第一个json调用失败会发生什么。也许没有其他人会执行。 – codin