How to pass a variable into a setTimeout function?
<p>I'm trying to set five staggered function calls (happening one second apart). That part works fine. What doesn't work is, I can't pass values 0 through 4 into the callback function. It just passes '5' each time. I can't seem to figure out why and how to fix it.</p>

<p>Code:</p>

<pre><code>​function callback(num)
{
    console.log(num);
}

for (var i = 0, loadDelay = 1000; i &lt; 5; ++ i, loadDelay += 1000)
    setTimeout(function() { callback(i); }, loadDelay);
</code></pre>

<p>Result:</p>

<pre><code>5
5
5
5
5
</code></pre>

<p>Desired result:</p>

<pre><code>0
1
2
3
4
</code></pre>
