Skip to content

Commit 0ce304c

Browse files
author
Garrett Johnson
committed
added closure examples sans-jquery
1 parent 25f4921 commit 0ce304c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

content/javascript-101/closures.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ function is defined, resulting in all clicks alerting 5.
1919
How to lock in the value of i?
2020

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

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

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

0 commit comments

Comments
 (0)