title: Fire event after 2 seconds using javascript/jQuery
body: <p>I have similar requirement like <a href="https://stackoverflow.com/questions/4364729/jquery-run-code-2-seconds-after-last-keypress">jquery-run-code-2-seconds-after-last-keypress</a>.</p>
  
  <p>Now, when I use <code>@brad</code> code, it works fine when i do :-</p>
  
  <pre><code>$('input.username').keypress(debounce(function (event) {
      console.log('Search keypress');
  }, 250));
  </code></pre>
  
  <p>But not when :-</p>
  
  <pre><code>$('#search').keypress(function () {
      debounce(function (event) {
          console.log('Search keypress');
     }, 2000);
  });
  </code></pre>
  
  <p><a href="http://jsfiddle.net/xQQ6h/7/" rel="nofollow noreferrer"><strong>Fiddle here</strong></a>. Any help would be really appretiated.</p>
  
  <p>Edit:-</p>
  
  <p>Initially I was doing something like below, which is not what I want-</p>
  
  <pre><code>$('#search').keypress(function () {
      setTimeout(function () {
        console.log('Search keypress');
      }, 3000);
  });
  </code></pre>
  
  <p>Edit 2:- I want something very similar to below:- <strong><em>(Copied text from there )</em></strong></p>
  
  <p>At the moment after 3 characters are entered it will search on every key press.</p>
  
  <p>What I want is</p>
  
  <blockquote>
    <p><strong>Case1: </strong><br>
    User enters tes<br>
    2 seconds pass
    search performed
    <hr />
    <strong>Case2: </strong><br>
    User enters tes<br>
    1 second pass<br>
    user presses t<br>
    2 seconds pass<br>
    search is performed on test<br>
    <hr />
    <strong>Case3: </strong><br>
    User enters tes<br>
    1 second passes<br>
    user presses t<br>
    1.5 seconds pass<br>
    user presses i<br>
    1.5 seconds pass<br>
    user presses n<br>
    1.5 seconds pass<br>
    user presses g<br>
    2 seconds pass<br>
    search in performed on testing  </p>
  </blockquote>
  
  <p>so as you can see,
  the search action is only performed when there has been no key presses(or pastes ect) in the two seconds following a key press.</p>
  
  <p>my basic idea is to.</p>
  
  <p>on keydown
  Call a function that sets a timeout,
  the function  also sets a variable containing the last entry in the textbox.
  when the timeout expires it checks the value in the box to see if it matches the value in the variable.</p>