Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your "another approach" is correct, this is how it's usually done.</p> <p>And as for the problem of <code>i</code> always being a constant, that's how closures work! You see, when you create this function that does something with <code>i</code> (like <code>function() { alert(i); }</code>), that function, as they say, <em>'captures'</em>, or <em>'binds'</em> the variable <code>i</code>, so that variable <code>i</code> does not die after the loop is finished, but continues to live on and is still referenced from that function.</p> <p>To demonstrate this concept, consider the following code:</p> <pre><code>var i = 5; var fn = function() { alert(i); }; fn(); // displays "5" i = 6; fn(); // displays "6" </code></pre> <p>When it is written in this way, the concept becomes a bit more evident, doesn't it? Since you're changing the variable in the loop, after the loop is finished the variable retains it's last value of <code>(1+steps)</code> - and that's exactly what your function sees when it starts executing.</p> <p>To work around this, you have to create another function that will return a function. Yes, I know, kind of mind-blowing, but bear with me. Consider the revised version of my example:</p> <pre><code>function createFn( theArgument ) { return function() { alert( theArgument ); }; } var i = 5; var fn = createFn( i ); fn(); // displays "5" i = 6; fn(); // still displays "5". Voila! </code></pre> <p>This works, because the <code>fn</code> function no longer <em>binds</em> the variable <code>i</code>. Instead, now it binds another variable - <code>theArgument</code>, which has nothing to do with <code>i</code>, other than they have the same value at the moment of calling <code>createFn</code>. Now you can change your <code>i</code> all you want - <code>theArgument</code> will be invincible.</p> <p>Applying this to your code, here's how you should modify it:</p> <pre><code>function createTimeoutHandler( elemArg, iDivStepsArg ) { return function() { setOpacity( elemArg, iDivStepsArg ); }; } for (var i = 1; i &lt;= steps; i++) { console.log(i/steps + ', ' + (i/steps) * duration); setTimeout( createTimeoutHandler( elem, i/steps ), (i/steps) * duration); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload