title: Jquery Run code 2 seconds after last keypress
body: <p>I am working on a auto search function for a site.</p>
  
  <p>It uses ajax to query an api.</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>
  
  <p>If they match then there has been no change.
  So do the search</p>
  
  <p>If they dont then do nothing, because the timeout from the subsequent key press will be following to do the same check.</p>
  
  <p>Is this the only/best way to do this?</p>