Javascript Closure question.

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-04-24T15:28:27Z Indexed on 2010/04/24 15:33 UTC
Read the original article Hit count: 240

Filed under:
|

Why the following code prints "0-100"?

(function () {
for ( var i = 100; i >= 0; i -= 5) {
    (function() {
        var pos = i;
        setTimeout(function() {
            console.log(" pos = " + pos);
        }, (pos + 1)*10);
    })();
}
})();

I declare pos = i , which should be in a descending order. This code originated from John Resig' fadeIn() function in his book Pro javascript techniques.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about closures