title: How to preserve setTimeout parameter value until execution?
body: <p>I have a bit of code that executes on keypress and saves data to a database as the user types. </p>
  
  <p>I added a setTimeout function with a clearTimeout infront of it so not EVERY single character the user enters is sending out an Ajax request to save data. </p>
  
  <p>While the setTimeout works great for one input field , if the user decides to switch input fields quickly (before the setTimeout delay is up) the parameter passed to the callSomeAjax changes before the function is executed. </p>
  
  <p>Simplified version of what's happening...</p>
  
  <pre><code>var a = 1; //some user data
  
  //function to call ajax script to save data to database
  function callSomeAjax(a)
  {
     console.log(a);
  }
  
  setTimeout(function(){callSomeAjax(a)},1000); //executes callSomeAjax after 1 second delay
  
  a=3; //change user data before callSomeAjax executes
  
  // Result of console.log is 3, not 1 like I want it to be...
  </code></pre>
  
  <p><a href="http://jsfiddle.net/U59Hv/" rel="noreferrer">Code on fiddle</a></p>
  
  <p>Any ideas?</p>