【问题标题】:Add Time to simple Firebase chatt为简单的 Firebase 聊天添加时间
【发布时间】:2014-06-21 00:45:25
【问题描述】:

我正在尝试在简单的 Firebase 聊天中添加时间指示,但我根本无法让它工作。

我已经尝试使用:

var time = new Date() 或
var time = new Date().getTime()

但似乎Firebase无法识别它。

有人知道如何在消息中打印时间吗?

提前致谢!

我的代码如下所示:

<!doctype html>
<html>
<head>
<script src='https://cdn.firebase.com/js/client/1.0.11/firebase.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
<style>


  </style>

<title>
 Live
</title>
</head>

 <header style="background: #1E2E3C; height:20px; min-width:640px">
 <p style="color: white; font-family: helvetica; margin: 0px 8px 0px 0px ;text- align:right;font-size:16px"><span title="Close Window to LOGOUT">Chat Messenger </span></p>

</header>

<body style="background: white; margin: 0px;padding:0px">



 <div id='messagesDiv' style="height:300px; min-width:600px; overflow:auto;  padding:10px;     margin:8px; background: #ECF2F6; color: #1E2E3C; font-family: helvetica;font-size:18px;border-    radius:4px;"></div>

  <div>
 <input type='text' id='nameInput' placeholder='User Name' style="width:200px;height:25px; margin:1px 0px 4px 8px; background: white;border-radius:4px; border: 3px solid #E6ECF2; font-family: helvetica; color: #1E2E3C"><br>

    <span title="Press ENTER to send message">
 <input type='text' id='messageInput' placeholder='Message' style="width:610px; height:25px; margin:4px 8px 8px 8px; background: white;border-radius:4px; border: 3px solid #E6ECF2; font-family: helvetica; color: #1E2E3C">
    </span>

  </div>




  <script>

  var myDataRef = new Firebase('https://xxxxxxxx.firebaseio.com');
  $('#messageInput').keypress(function (e) {
    if (e.keyCode == 13) {
      var name = $('#nameInput').val();
      var text = $('#messageInput').val();

      myDataRef.push({name: name, text: text});
      $('#messageInput').val('');
    }
  });
  myDataRef.on('child_added', function(snapshot) {
    var message = snapshot.val();
    displayChatMessage(message.name, message.text);
  });
  function displayChatMessage(name, text) {
   $('<div/>').text(text).prepend($('<em/>').text(name+': ' )).appendTo($('#messagesDiv'));
    $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
  };
</script>

【问题讨论】:

    标签: javascript firebase


    【解决方案1】:

    从 Firebase 获取时间的一种方法是在您 .push() 发送消息时使用 Firebase.ServerValue.TIMESTAMP

    点赞myDataRef.push({name: name, text: text, time: Firebase.Server.TIMESTAMP});

    由于Firebase Timestamp是自UNIX纪元以来的时间,以毫秒为单位,你需要在使用它之前对其进行转换。此功能可能会帮助您:

    time = function (timestamp) { // Convert UNIX epoch time into human readble time.
        var epoch = new Date(timestamp);
        var date = epoch.toUTCString();
        return date;
    }
    

    最后,你的显示逻辑可能是这样的:

    myDataRef.on('child_added', function(snapshot) {
    var message = snapshot.val();
    var t = time(message.time);
    displayChatMessage(message.name, message.text, t);
    });
    

    【讨论】:

    • 感谢您的帮助,我能够让它工作。我只需要在 .push 函数中进行一点更改,我必须使用 .ServerValue.TIMESTAMP 才能使其工作。再次感谢您花时间这么详细地回答!
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    相关资源
    最近更新 更多