title: "User is typing" function in chat
body: <p>I'm trying to add in chat a "user is typing" function;  chat  written in PHP + MySQL/Ajax. </p>
  
  <p>How it should work: </p>
  
  <p>-when my chat partner X starts typing I see in my chat box: "X is typing"</p>
  
  <p>-when I (named Y) am typing he sees in his chat box: "Y is typing" (just like Yahoo Messenger).</p>
  
  <p>The code I've tried so far:</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
      var timer = 0;
  
      function reduceTimer() {
          timer = timer - 1;
          isTyping(true);
      }
  
      function isTyping(val) {
          if (val == 'true') {
              document.getElementById('typing_on').innerHTML = "User is typing...";
          } else {
  
              if (timer &lt;= 0) {
                  document.getElementById('typing_on').innerHTML = "No one is typing -blank space.";
              } else {
                  setTimeout("reduceTimer();", 500);
              }
          }
      }
  &lt;/script&gt;
  
  &lt;label&gt;
      &lt;textarea onkeypress="isTyping('true'); timer=5;" onkeyup="isTyping('false')" name="textarea" id="textarea" cols="45" rows="5"&gt;&lt;/textarea&gt;
  &lt;/label&gt;
  &lt;div id="typing_on"&gt;No one is typing -blank speace.&lt;/div&gt;
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>If I stop for a few seconds to think about my spelling it looks like I've stopped typing. Is there a more relevant and less complicated way to set this function? Is there possible a code for:</p>
  
  <ul>
  <li>text box not empty (user pressed any key so started typing) -> Message: User is typing.</li>
  <li>text box empty -> Message: User is not typing.</li>
  <li>text box not empty, but user hasn't pressed any key for longer than 5 seconds -> Message: User is not typing.</li>
  </ul></li>
  <li><p>It shows to myself that I am typing; how could I implement it or where, in order to see in my chat box the "X user is typing" and not "Myself is typing". Same for the other user, he should get a message about me typing/not typing , not about himself.</p></li>
  </ol>
  
  <p>Thank you.</p>