title: Specific textarea behaviour related to setTimeout
body: <p>what I am trying to do is give the user a textarea (with enabled input) and <strong>allow him to write up to 10 chars.</strong> Actually, <strong>the textarea should ALLOW more chars</strong>, not only 10, and the other chars would behave accordingly:</p>
  
  <p>1º <strong>There is a setTimeout whenever the user is on the 11ºchar</strong>. So, I'm writing something there, I reach the char number 10º (the "maximum" allowed of text) and want that other char to be erased AFTER a given time.</p>
  
  <pre><code>somethinge = 10 chars.
  anotherwor = 10 chars.
  
  input: somethingeiefjaiehf
  after 5 seconds:
  input: somethinge
  
  input: anotherwordtotestthisandseeifitworks
  after 5 seconds:
  input: anotherwor
  </code></pre>
  
  <p>2ºTo accomplish this <strong>I basically attached a clearTimeout to a global variable:</strong></p>
  
  <pre><code>//Function "countChar(val)" is activated with the keydown event
      var timer_to_op = null;
      function countChar(val) {
              var len = val.value.length; //lenght of input in textarea
              clearTimeout(timer_to_op);
      //...
      //irrelevant code
      //...
                        }else if(len &gt; 140){
                          $("#status_toOp_num").text(145-len);
                          timer_to_op = setTimeout(function(){
                              val.value = val.value.substring(0, 140);
                          },5000);
  
      }
  </code></pre>
  
  <p>Actually, for some reason, it won't work. <strong>If the user is typing AND he types another char within the 5 seconds then I want the timer to restart.</strong> </p>
  
  <pre><code>input: anotherwor  
  input: anotherword (+d) timer = 1...2...3... 
  input: anotherworde (+e) timer = 1...2...  
  input: anotherwordef (+f) timer = 1...2...3...4...5!  
  input: anotherwor     the user took more than 5 seconds so it erased the excedent.
  </code></pre>
  
  <p>Hope I got my point through. Any ideas on this one? Thank you very much! (I didn't put any html, it's only <code>&lt;textarea onclick="countChar(this)"&gt;</code> )</p>