title: AJAX: Delay for search on typing in form field
body: <p>On my website I use JavaScript/AJAX to do the search and show results while the user is still typing.</p>
  
  <p><strong>HTML (body):</strong></p>
  
  <pre><code>&lt;form action="" method="post" accept-charset="utf-8"&gt;
      &lt;p&gt;&lt;input type="text" name="q" id="q" value="" onkeyup="doSearch(this.value)" /&gt;&lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>JavaScript (header):</strong></p>
  
  <pre><code>function doSearch(text) {
      // do the ajax stuff here
      // call getResults.php?search=[text]
  }
  </code></pre>
  
  <p>But this could cause a lot of requests coming to the server.</p>
  
  <p><strong>Thus I want to relieve the server by setting up a delay:</strong></p>
  
  <p>Whenever the onkeyup event is fired, the function doSearch() should show an "ajax loading graphic" and wait for 2 seconds. Only if the event is NOT fired again during these 2 seconds, the results should be fetched from the PHP file.</p>
  
  <p>Is there any way to do this? Could you help me please? Thanks in advance!</p>