用户发送信息时如何停止“正在输入”功能

时间:2021-05-19 03:36:12

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);
            }
        }
        else if(e.which == 13 && $("#msg").val() !== "") {
          $("#"+data.person+"").remove();
        }
      });

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

1 个解决方案

#1


3  

You are simply just missing the case when the user hits the Enter key. You need to add to your if statement.

当用户按下Enter键时,您只是错过了这种情况。你需要添加到if语句中。

$("#msg").keypress(function (e) {
    if (e.which !== 13) {
        typing = true; // we know the user is typing because they have pressed a key but not 'Enter'
        socket.emit("typing", true);
        clearTimeout(timeout);
        timeout = setTimeout(timeoutFunction, 1500);
    } else {
        clearTimeout(timeout); // no need to fire the timeoutFunction twice (as we do it on the next line)
        timeoutFunction(); // probably needs a better name but this will immediately send the necessary `socket.emit('typing', false)` when the enter key is pressed
    }
});

#1


3  

You are simply just missing the case when the user hits the Enter key. You need to add to your if statement.

当用户按下Enter键时,您只是错过了这种情况。你需要添加到if语句中。

$("#msg").keypress(function (e) {
    if (e.which !== 13) {
        typing = true; // we know the user is typing because they have pressed a key but not 'Enter'
        socket.emit("typing", true);
        clearTimeout(timeout);
        timeout = setTimeout(timeoutFunction, 1500);
    } else {
        clearTimeout(timeout); // no need to fire the timeoutFunction twice (as we do it on the next line)
        timeoutFunction(); // probably needs a better name but this will immediately send the necessary `socket.emit('typing', false)` when the enter key is pressed
    }
});