Skip to content

Commit

Permalink
added closure examples sans-jquery
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Johnson committed Jun 4, 2011
1 parent 25f4921 commit 0ce304c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions content/javascript-101/closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function is defined, resulting in all clicks alerting 5.
How to lock in the value of i?

/* this won't behave as we want it to; */
/* every click will alert 5 */
/* every 100 milliseconds, 5 will alert */
for (var i=0; i<5; i++) {
$('<p>click me</p>').appendTo('body').click(function() {
alert(i);
});
setTimeout(function() {
alert(i);
}, i*100);
}
</div>

Expand All @@ -36,7 +36,7 @@ Locking in the value of i with a closure
};

for (var i=0; i<5; i++) {
$('<p>click me</p>').appendTo('body').click(createFunction(i));
setTimeout( createFunction(i), i*100 );
}
</div>

Expand Down

0 comments on commit 0ce304c

Please sign in to comment.