当前时间上的Firebase触发器

当前时间上的Firebase触发器,firebase,Firebase,我用事件创建类似日历的东西。 我想在事件发生时为用户弹出消息 我的解决方案: //this code runs in loop var offsetRef = new Firebase("https://example.firebaseio-demo.com/.info/serverTimeOffset"); offsetRef.on("value", function(snap) { var offset = snap.val(); var estimatedServerTim

我用事件创建类似日历的东西。 我想在事件发生时为用户弹出消息

我的解决方案:

//this code runs in loop
var offsetRef = new Firebase("https://example.firebaseio-demo.com/.info/serverTimeOffset");
offsetRef.on("value", function(snap) {
    var offset = snap.val();
    var estimatedServerTimeMs = new Date().getTime() + offset;
    fbRef.endAt(estimatedServerTimeMs).once("value", function(ss) {/*remove old events*/});
});
这样的事情(没有循环)是不可能的吗


感谢您的回复

最好只计算一次serverTimeOffset,然后在知道当前时间(大约)后,将弹出窗口包装在setTimeout中:


谢谢您的回复,但我不明白如何将您的代码与我的代码结合起来:(对我来说,这一行很重要:fbRef.endAt(estTime)。once(“value”,function(ss){/*remove old events*/});如何或从何处在您的代码中从firebase获取事件?感谢您始终可以单独运行endAt(estTime)。once(…)来删除旧事件。我的代码片段只处理在将来创建事件提示。
fbRef.endAt(Firebase.ServerValue.TIMESTAMP).on("child_added", function(snap) {/*...*/});
var timeouts = [];
offsetRef.on("value", function(snap) {
  // Cancel all previous timeouts.
  timeouts.forEach(function(i) { clearTimeout(i); });
  var estTime = new Date().getTime() + snap.val();
  var promptTime = /* Get time at which you want to prompt */
  timeouts.push(setTimeout(function showPrompt() {
    ...
  }, promptTime));
});