Skip to content

Instantly share code, notes, and snippets.

@rileydutton
Created November 11, 2015 15:04
Show Gist options
  • Save rileydutton/0d7a470e658764fe15e9 to your computer and use it in GitHub Desktop.
Save rileydutton/0d7a470e658764fe15e9 to your computer and use it in GitHub Desktop.
// 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();
// 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