Firebase活动“<用户>正在写...“像WhatsApp [英] Firebase event "<user> is writing..." like WhatsApp

查看:100
本文介绍了Firebase活动“<用户>正在写...“像WhatsApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方式在Firebase事件中克隆whatsapp事件 [user]正在撰写...

阅读 https://www.firebase.com/docs/web/api/上的Firebase活动但是我还没有发现任何问题。
$ b $ p

谢谢。

解决方案

  var ref = new Firebase('https: //<your-app>.firebaseio.com'); 
var input = document.getElementById('input');
var typers = document.getElementById('typers');
var uid = Date.now(); //产生一个假的用户ID
var timer;

//附加一个侦听器,显示当前在列表中输入的所有人员
ref.on('value',function(snapshot){
typers.innerText ='';
snapshot.forEach(function(typer){
var li = document.createElement('li');
li.innerText = typer.key();
typers.appendChild (li);
});
});

//每当textarea的内容改变时
input.addEventListener('input',function(e){
//标记这个用户一个输入
ref.child(uid).set(true);
//如果我们倒计时,停止定时器
if(timer)clearTimeout(timer);
//删除用户在2秒内
timer = setTimeout(function(){
ref.child(uid).remove();
},2000);
});

若要查看它,请查看这个JSBin
我宣布的 tweet


Is there any manner to clone the whatsapp event "[user] is writing..." in Firebase events?

I have reading about the Firebase events in https://www.firebase.com/docs/web/api/ but I've not found anything about the issue.

Thanks.

解决方案

I wrote such a typing indicator a while ago.

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);
});

To see it in action, have a look at this JSBin. The tweet where I announced it.

这篇关于Firebase活动“&lt;用户&gt;正在写...“像WhatsApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆