gpt4 book ai didi

javascript - 使用 Javascript 在 Firebase 中中断循环

转载 作者:行者123 更新时间:2023-11-30 15:35:48 24 4
gpt4 key购买 nike

我是 Firebase 初学者,在打破循环方面遇到了麻烦。下面的代码查看配方产品和产品数量,如果库存有足够的产品来制作配方,代码会触发警报,说明有足够的产品,否则会触发警报,说明库存产品不足足够的。我想打破 else 的循环,我该怎么做?

checkFirebase.orderByKey().limitToLast(20).on('child_added', function(snapshot){

var productRecipe = snapshot.key;
var amountRecipe = snapshot.child("amount").val() * amountAdded;

if(productRecipe == productStock){

if(amountRecipe <= amountStock){
alert("The amount is enough.");
}else{
alert("The quantity is not enough.");
break;
}

}

});

最佳答案

如果你想停止监听child_added事件,你可以试试Off

试试这个:

checkFirebase.orderByKey().limitToLast(20).on('child_added', function(snapshot){

var productRecipe = snapshot.key;
var amountRecipe = snapshot.child("amount").val() * amountAdded;

if(productRecipe == productStock){

if(amountRecipe <= amountStock){
alert("The amount is enough.");
}else{
alert("The quantity is not enough.");
checkFirebase.off('child_added');//stop listening to child_added event
}

}

});

有关off的更多信息,请阅读firebase References#off Documentation

关于javascript - 使用 Javascript 在 Firebase 中中断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41560642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com