
JavaScript closures and recursion - ecounysis
http://stackoverflow.com/questions/4273617/is-there-a-way-to-make-this-slideshow-move-automatically/4273831#4273831
======
tlrobinson
I'm sorry, what's so special about this bit of code?

It doesn't seem very interesting unless you weren't aware of JavaScript's
functional nature, and it's actually more complicated than it needs to be,
unless I'm missing something. Here's how I'd do it:

    
    
        function startSlideshow(ms) {
            var index = -1;
            var count = $(".change_link").length - 1;
            return setInterval(function() {
                index = (index + 1) % count;
                $('.slideshow').blinds_change(index);
            }, ms);
        }

~~~
synx508
I was also rather confused as to what I was supposed to be looking at and why
it wasn't using setInterval.

Your script is nicer, the modulo and increment in the same line is very tidy.
It's not hacker news though, is it, or am I missing something?

------
ecounysis
I suppose there's nothing too special abot the code. I have never seen a very
good explanation of closures in javascript, so I thought I'd share it for
anyone else who might benefit from it. You are more than welcome to not be
impressed. In which case you are not likely the intended audience.

~~~
camtarn
Closures did indeed blow my mind when I first decided to properly look into
them, having originally assumed that JavaScript was "just like a very limited
Java with a couple of weird syntax quirks" ;) Ah, the perils of teaching
yourself a language using really, really old web tutorials. I do wish
something like PromoteJS.com had been around when I was learning...

