Created
November 11, 2015 15:04
-
-
Save rileydutton/0d7a470e658764fe15e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define a function that increments a counter in a loop | |
function closureExample() { | |
var i = 0; | |
for (i = 0; i< 3 ;i++) { | |
setTimeout(function() { | |
console.log('counter value is ' + i); | |
}, 1000); | |
} | |
} | |
// call the example function | |
closureExample(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define a function that increments a counter in a loop | |
function closureExample() { | |
var i = 0; | |
for (i = 0; i< 3 ;i++) { | |
(function (count) { | |
setTimeout(function() { | |
console.log('counter value is ' + count); | |
}, 1000); | |
}(i)); | |
} | |
} | |
// call the example function | |
closureExample(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment