我刚刚了解JavaScript中的链接'然后',但我面临一个问题,即最后一个块然后执行前一个然后块已完成。 相关的代码是:被链接,然后不正确的顺序
firebaseRef.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
//getting key of the child
var pid=childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
var name=childData.name;
pids.push(pid);
names.push(name);
});
})
.then(function() {
for (var i = 0; i < pids.length; i++) {
$("#pid"+(i+1)).text(pids[i]);
$("#name"+(i+1)).text(names[i]);
}
set1();
set2();
set3();
set4();
})
.then(function(){
calcAvg();
});
这里,calcAvg()的SET1(),设定2(),SET3(),SET4()方法已经执行之前触发关闭。这给了我不正确的结果。任何人都可以帮我解决这个问题吗?
编辑1:请检查checkAvg()方法在这里:https://jsfiddle.net/jek9m7hn/。看起来像是由于它的一些问题。
'set1-4()'和'calcAvg()'做了什么?你可以发布此代码供我们检查吗? – gyre
请检查https://jsfiddle.net/jek9m7hn/。我已经添加了set1()和calcAvg()的代码。 set2(),set3(),set4()与set1()类似。如果您发现任何问题,请告诉我。 –
@torazaburo我试着在calcAvg中放入一个alert语句,并看到在setn函数之前执行的警报。另外,我在setn()中设置了值,然后在calcAvg()中计算它们的平均值。由于calcAvg在setn执行之前执行,所以我得到的平均值为NaN。 –