title: Passing functions to setTimeout in a loop: always the last value?
body: <p>I'm trying to use setTimeout to execute an anonymous function that I pass information into and I'm having trouble. This (hard-coded version) would work just fine:</p>
  
  <pre><code>setTimeout(function(){alert("hello");},1000);
  setTimeout(function(){alert("world");},2000);
  </code></pre>
  
  <p>But I'm trying to take the hello and world from an array and pass them into the function without (a) using global variables, and (2) using eval. I know how I could do it using globals or eval, but how can I do it without. Here is what I'd like to do (but I know it won't work):</p>
  
  <pre><code>var strings = [ "hello", "world" ];
  var delay = 1000;
  for(var i=0;i&lt;strings.length;i++) {
      setTimeout( function(){alert(strings[i]);}, delay);
      delay += 1000;
  }
  </code></pre>
  
  <p>Of course strings[i] will be out of context. How can I pass strings[i] into that anonymous function without eval or globals?</p>