Firebase活动"<user>正在写......"就像WhatsApp一样

nac*_*rin 1 firebase firebase-realtime-database

有没有办法在Firebase事件中克隆whatsapp事件" [user]正在编写... "?

我在https://www.firebase.com/docs/web/api/上阅读了有关Firebase活动的信息,但我没有发现任何有关此问题的信息.

谢谢.

Fra*_*len 5

我刚才写了这样一个打字指示器.

var ref = new Firebase('https://<your-app>.firebaseio.com');
var input = document.getElementById('input');
var typers = document.getElementById('typers');
var uid = Date.now(); // generate a fake user id
var timer;

// attach a listener that display all people current typing in a list
ref.on('value', function(snapshot) {
  typers.innerText = '';
  snapshot.forEach(function(typer) {
    var li = document.createElement('li');
    li.innerText = typer.key();
    typers.appendChild(li);
  });
});

// whenever the content of the textarea changes
input.addEventListener('input',function(e) {
  // mark this user a "typing"
  ref.child(uid).set(true);
  // if we're counting down, stop the timer
  if (timer) clearTimeout(timer);
  // remove this user in 2 seconds
  timer = setTimeout(function() {
    ref.child(uid).remove();
  }, 2000);
});
Run Code Online (Sandbox Code Playgroud)

要查看它的实际效果,请查看此JSBin.我宣布它推文.