We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 25f4921 commit 0ce304cCopy full SHA for 0ce304c
content/javascript-101/closures.md
@@ -19,11 +19,11 @@ function is defined, resulting in all clicks alerting 5.
19
How to lock in the value of i?
20
21
/* this won't behave as we want it to; */
22
- /* every click will alert 5 */
+ /* every 100 milliseconds, 5 will alert */
23
for (var i=0; i<5; i++) {
24
- $('<p>click me</p>').appendTo('body').click(function() {
25
- alert(i);
26
- });
+ setTimeout(function() {
+ alert(i);
+ }, i*100);
27
}
28
</div>
29
@@ -36,7 +36,7 @@ Locking in the value of i with a closure
36
};
37
38
39
- $('<p>click me</p>').appendTo('body').click(createFunction(i));
+ setTimeout( createFunction(i), i*100 );
40
41
42
0 commit comments