Skip to content

Instantly share code, notes, and snippets.

@awilson28
Last active March 11, 2018 17:42

Revisions

  1. awilson28 revised this gist Mar 13, 2015. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -8,11 +8,10 @@ for (var i = 0; i <= 3; i++) {



    /* fixing this with an Immediately Invoked Function Expression (IFFE)
    The use of an IIFE inside each iteration creates a new scope
    for each iteration, which yields timeout function callbacks
    a new scope for each iteration: each scope has a variable i with
    the right per-iteration value in it for us to access */
    /* fixing this with an Immediately Invoked Function Expression (IIFE)
    The IIFE creates a new scope for each iteration,
    providing the callback in setTimeout with a new variable i that is
    mapped to the right per-iteration value */

    for (var j = 0; j <= 3; j++) {
    (function(k){
    @@ -24,7 +23,7 @@ for (var j = 0; j <= 3; j++) {



    /* so we define an IIFE that takes a variable k and then console.log's that k
    /* above, we define an IIFE that takes a parameter k and then console.log's that k
    and then we invoke that IIFE with j: the IIFE closes over j so that the
    callback function has a copy of j at 0, 1, 2, and 3. Note that we can also
    just keep j everywhere as shown in the code directly below: */
  2. awilson28 revised this gist Mar 13, 2015. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,9 @@ for (var i = 0; i <= 3; i++) {

    /* fixing this with an Immediately Invoked Function Expression (IFFE)
    The use of an IIFE inside each iteration creates a new scope
    for each iteration, which gives our timeout function callbacks
    the opportunity to close over a new scope for each iteration, one which
    had a variable with the right per-iteration value in it for us to access */
    for each iteration, which yields timeout function callbacks
    a new scope for each iteration: each scope has a variable i with
    the right per-iteration value in it for us to access */

    for (var j = 0; j <= 3; j++) {
    (function(k){
    @@ -47,4 +47,6 @@ for (var i = 0; i <= 3; i++) {
    }, 1000)
    })();
    }


    ```
  3. awilson28 revised this gist Mar 2, 2015. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,12 @@ for (var i = 0; i <= 3; i++) {



    //fixing this with an Immediately Invoked Function Expression (IFFE)
    /* fixing this with an Immediately Invoked Function Expression (IFFE)
    The use of an IIFE inside each iteration creates a new scope
    for each iteration, which gives our timeout function callbacks
    the opportunity to close over a new scope for each iteration, one which
    had a variable with the right per-iteration value in it for us to access */

    for (var j = 0; j <= 3; j++) {
    (function(k){
    setTimeout(function(){
    @@ -17,6 +22,8 @@ for (var j = 0; j <= 3; j++) {
    })(j)
    }



    /* so we define an IIFE that takes a variable k and then console.log's that k
    and then we invoke that IIFE with j: the IIFE closes over j so that the
    callback function has a copy of j at 0, 1, 2, and 3. Note that we can also
    @@ -32,10 +39,6 @@ for (var j = 0; j <= 3; j++){

    //We can achieve the same functionality like so:

    /* The use of an IIFE inside each iteration creates a new scope
    for each iteration, which gives our timeout function callbacks
    the opportunity to close over a new scope for each iteration, one which
    had a variable with the right per-iteration value in it for us to access */
    for (var i = 0; i <= 3; i++) {
    (function() {
    var j = i;
  4. awilson28 revised this gist Mar 2, 2015. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,17 @@ for (var j = 0; j <= 3; j++) {
    }

    /* so we define an IIFE that takes a variable k and then console.log's that k
    and then we invoke that IIFE with j the IIFE closes over j so that the
    callback function has a copy of j at 0, 1, 2, and 3 */
    and then we invoke that IIFE with j: the IIFE closes over j so that the
    callback function has a copy of j at 0, 1, 2, and 3. Note that we can also
    just keep j everywhere as shown in the code directly below: */

    for (var j = 0; j <= 3; j++){
    (function(j) {
    setTimeout(function() {
    console.log(j);
    }, 1000)
    })(j)
    }

    //We can achieve the same functionality like so:

  5. awilson28 revised this gist Mar 2, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,10 @@ callback function has a copy of j at 0, 1, 2, and 3 */

    //We can achieve the same functionality like so:

    /* The use of an IIFE inside each iteration creates a new scope
    for each iteration, which gives our timeout function callbacks
    the opportunity to close over a new scope for each iteration, one which
    had a variable with the right per-iteration value in it for us to access */
    for (var i = 0; i <= 3; i++) {
    (function() {
    var j = i;
  6. awilson28 revised this gist Mar 2, 2015. 1 changed file with 19 additions and 6 deletions.
    25 changes: 19 additions & 6 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -10,12 +10,25 @@ for (var i = 0; i <= 3; i++) {

    //fixing this with an Immediately Invoked Function Expression (IFFE)
    for (var j = 0; j <= 3; j++) {
    (function(k){setTimeout(function(){
    console.log(k);
    }, 0)})(j)
    (function(k){
    setTimeout(function(){
    console.log(k);
    }, 0)
    })(j)
    }

    //so we define an IIFE that takes a variable k and then console.log's that k
    //and then we invoke that IIFE with j
    //the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3
    /* so we define an IIFE that takes a variable k and then console.log's that k
    and then we invoke that IIFE with j the IIFE closes over j so that the
    callback function has a copy of j at 0, 1, 2, and 3 */

    //We can achieve the same functionality like so:

    for (var i = 0; i <= 3; i++) {
    (function() {
    var j = i;
    setTimeout(function() {
    console.log(j)
    }, 1000)
    })();
    }
    ```
  7. awilson28 revised this gist Jan 22, 2015. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -8,10 +8,14 @@ for (var i = 0; i <= 3; i++) {



    //fixing this with an IFFE
    for (var i = 0; i <= 3; i++) {
    (function(i){setTimeout(function(){
    console.log(i);
    }, 0)})(i)
    //fixing this with an Immediately Invoked Function Expression (IFFE)
    for (var j = 0; j <= 3; j++) {
    (function(k){setTimeout(function(){
    console.log(k);
    }, 0)})(j)
    }

    //so we define an IIFE that takes a variable k and then console.log's that k
    //and then we invoke that IIFE with j
    //the IIFE closes over j so that the callback function has a copy of j at 0, 1, 2, and 3
    ```
  8. awilson28 revised this gist Jan 22, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,13 @@ for (var i = 0; i <= 3; i++) {
    console.log(i);
    }, 0);
    }



    //fixing this with an IFFE
    for (var i = 0; i <= 3; i++) {
    (function(i){setTimeout(function(){
    console.log(i);
    }, 0)})(i)
    }
    ```
  9. awilson28 created this gist Jan 21, 2015.
    8 changes: 8 additions & 0 deletions setTimeout.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    ``` javascript
    //run this in your console
    for (var i = 0; i <= 3; i++) {
    setTimeout(function(){
    console.log(i);
    }, 0);
    }
    ```