【发布时间】:2018-01-06 01:48:00
【问题描述】:
我有一个实时计分服务器,可以在创建游戏时更新 firebase 中的分数。目前,节点服务器有一个监听器,下面的代码专门引用了带有 firebase 的节点类型。
ref.orderByChild("league").equalTo('NBA').on("value", function(snapshot) {
refreshInterval = setInterval(function() {
nbafile.nbaUpdate(snapshot.val());
}, 5000);
}, function(errorObject) {});
};
就目前而言,当在 firebase 中创建游戏时,计时器会设置为每 5 秒更新一次游戏,并在游戏状态关闭后停止更新。
问题来了:
如果我尝试为另一项运动(例如 NCAAB)添加一个额外的监听器,它要么永远不会被激活,要么它们都被调用但计时器不会停止。
ref.orderByChild("league").equalTo('NCAAB').on("value", function(snapshot) {
refreshInterval = setInterval(function() {
cbbfile.cbbUpdate(snapshot.val());
}, 5000);
}, function(errorObject) {});
}
先调用哪个函数会优先,而另一个函数不会触发。
下面是一段完整的代码以及游戏指向的 if 语句。
const startUpdate = function(db, ref) {
db = admin.database();
exports.db = db;
ref = db.ref('game');
exports.ref = ref;
console.log('-----------------------------------');
ref.orderByChild("league").equalTo('NBA').on("value", function(snapshot) {
refreshInterval = setInterval(function() {
nbafile.nbaUpdate(snapshot.val());
}, 5000);
}, function(errorObject) {});
};
const startUpdate2 = function(db, ref) {
db = admin.database();
exports.db = db
ref = db.ref('game');
exports.ref = ref;
console.log('-----------------------------------');
ref.orderByChild("league").equalTo('NCAAB').on("value", function(snapshot) {
refreshInterval = setInterval(function() {
cbbfile.cbbUpdate(snapshot.val());
}, 5000);
}, function(errorObject) {});
}
nbaupdate.js
let nbaUpdate = function(games) {
console.log("Professional Basketball");
if (games === null) {
console.log('Games object empty');
return;
}
for (var id in games) {
if (games[id].league === 'NCAAB');
if (ts >= games[id].scheduledTimeUnix);
if ((games[id].status == 'complete') || (games[id].status == 'closed')) {
sports.clearTimer();
console.log(games[id].status)
} else {
【问题讨论】:
标签: javascript node.js firebase firebase-realtime-database