You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Each function executed within the loop will reference
// the last value stored in i (5).
// This won't behave as we want it to - every 100 milliseconds, 5 will alert
for ( var i = 0; i < 5; i++ ) {
setTimeout(function() {
alert( i );
}, i * 100 );
}
In Javascript-101 in the closures chapter, the setTimeout method is used before it's introduced what that is. This example would be easier to understand if it used something different that had already been introduced in previous chapters.
I haven't been able to think of an alternative example that exhibits the behavior that the existing example does (but this likely stems from being in the process of learning js myself).
The text was updated successfully, but these errors were encountered:
Agreed. Would you consider setTimeout as being part of the javascript 'basics'? If so, I'il PR an explanation. If not, I'il remove the reference to it in a PR.
For myself (as a n00b to javascript) going through the docs from the beginning, I didn't know what setTimeout was and to understand the example needed to go and find a reference outside the jquery docs, figure out what the method was and what it did, then try to re-interpret the docs. I'm still new enough to javascript to not actually know how core/essential/frequently used setTimeout is to know if it should be explained or removed. I'd defer to you or anyone who's a more experienced js developer to know how fundamental setTimeout is.
I'd opt to get it out the example right there. Later on, an extra post on Javascript' timing (setTimeout, but also setInterval) inside of Javascript 101 maybe?
https://github.com/jquery/learn.jquery.com/blob/master/page/javascript-101/closures.md
In Javascript-101 in the closures chapter, the
setTimeout
method is used before it's introduced what that is. This example would be easier to understand if it used something different that had already been introduced in previous chapters.I haven't been able to think of an alternative example that exhibits the behavior that the existing example does (but this likely stems from being in the process of learning js myself).
The text was updated successfully, but these errors were encountered: