“User is typing” function in chat

前端 未结 2 1511
渐次进展
渐次进展 2020-12-08 06:06

I\'m trying to add in chat a \"user is typing\" function; chat written in PHP + MySQL/Ajax.

How it should work:

-when my chat partner X starts typing I s

相关标签:
2条回答
  • I have created a fiddle that might be helpful to you. The idea is to refresh activity message using javascript setInterval function.

    var textarea = $('#textarea');
    var typingStatus = $('#typing_on');
    var lastTypedTime = new Date(0); // it's 01/01/1970, actually some time in the past
    var typingDelayMillis = 5000; // how long user can "think about his spelling" before we show "No one is typing -blank space." message
    
    function refreshTypingStatus() {
        if (!textarea.is(':focus') || textarea.val() == '' || new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
            typingStatus.html('No one is typing -blank space.');
        } else {
            typingStatus.html('User is typing...');
        }
    }
    function updateLastTypedTime() {
        lastTypedTime = new Date();
    }
    
    setInterval(refreshTypingStatus, 100);
    textarea.keypress(updateLastTypedTime);
    textarea.blur(refreshTypingStatus);
    
    0 讨论(0)
  • 2020-12-08 06:24
         <script>
            function isTyping() {
                    document.getElementById('typing_on').innerHTML = "User is typing...! "; }
    
            function  notTyping (){
           document.getElementById('typing_on').innerHTML = "No one is typing ! "; }
        </script>
    
        <label>
            <textarea onkeypress="setTimeout(isTyping(),4000); setInterval(notTyping,5000)"  name="textarea" id="textarea" cols="45" rows="5"></textarea>
        </label>
        <div id="typing_on">No one is typing -blank speace.</div> 
    
    
    
    
        <!DOCTYPE html>
        <html>
           <head>
           </head>
           <body>
    
          <script>
            function isTyping() {
                    document.getElementById('typing_on').innerHTML = "User is typing...! "; }
                
            function  notTyping (){
           document.getElementById('typing_on').innerHTML = "No one is typing ! "; }
        </script>
    
        <label>
            <textarea onkeypress="setTimeout(isTyping(),4000); setInterval(notTyping,5000)"  name="textarea" id="textarea" cols="45" rows="5"></textarea>
        </label>
        <div id="typing_on">No one is typing -blank speace.</div> 
           </body>
        </html>
    0 讨论(0)
提交回复
热议问题