Friday 21 April 2017

How to stop 'is typing' function when user sends message

My 'user is typing' function does not remove the 'is typing', when the user hits enter. So even though a user sends the message, the 'is typing' still displays, until the timer runs out.

Any ideas?

This is my code:

client-side

// Detect typing

      function timeoutFunction() {  
        typing = false;
        socket.emit("typing", false);
        socket.emit("notTyping", true)
      }

      $("#msg").keypress(function(e){
        if (e.which !== 13) {
          if (typing === false && $("#msg").is(":focus")) {
          typing = true;
          socket.emit("typing", true);
          } else {
              clearTimeout(timeout);
              timeout = setTimeout(timeoutFunction, 1500);
            }
          }
      });

      socket.on("isTyping", function(data) {  
        if (data.isTyping) {
          if ($("#"+data.person+"").length === 0) {
            socket.emit("checkTypingFunction");
            $("#chat").append("<div id='"+ data.person +"'><span class='grey'>" + data.person + " is typing...</div>");
            timeout = setTimeout(timeoutFunction, 1500);
          }
        } else {
           $("#"+data.person+"").remove();
          }
      });

server-side:

client.on("typing", function(data) {  
    if (typeof people[client.id] !== "undefined")
    socket.sockets.in(client.room).emit("isTyping", {isTyping: data, person: people[client.id].name});
    client.broadcast.to(client.room).emit("isTyping", {isTyping: data, person: people[client.id].name});
console.log("Someone is typing");
});



via JonasSH

No comments:

Post a Comment