Recursion in Javascript?

Given the following psudo-code (which is taken from my remote camera robot project)

[event handler](when event happens) {
   [do some startup and init code]
   start_everything_going();
]

// This block does something that will interact with the browser every 1/60 second.
def do_something() {
    [code that does something goes here]
   requestAnimationFrame(do_something);
}

def start_everything_going() {
    requestAnimationFrame(do_something);
}

The way this works, using requestAnimationFrame is that the event handler triggers, (say when a joystick is attached or activated), and after some setup or configuration code is run, it calls the initial function to begin the cyclical animation - start_everything_going().

Since requestAnimationFrame runs once and once only when called, it has to be repeatedly called to create a continuous effect.

The canonical method - and the one used in every example I have seen - is that the first invocation is done by a wrapper function called (eventually) by the event handler that triggers it.

In order to repeatedly call requestAnimationFrame, the animating function calls requestAnimationFrame, with itself as the argument, as the last thing before exiting.

A function calling itself is classic recursion, which is very common in programming when a complex algorithm that needs to repeat itself, (like calculating a mathematical series or many digits of an irrational number like π).  Eventually some ending condition is reached and the recursion begins to “unwind,” going backwards up the recursion “ladder” so to speak, until the very first function call is reached, where it returns the answer you requested.

As far as I remember, a recursive call to a function creates yet another instance of the function, (leaving the original instance suspended in time somewhere), repeating the steps needed to do something.  And this continues forever, creating additional instances of itself until, eventually, some ending condition is reached.

Since, in this case, the called function NEVER RETURNS, there is no way to “unwind” the recursion.  And, as far as I know, this will continue until either Hell freezes solid or all available computer resources have been exhausted.

Since this animation refreshes itself every 1/60th second, and can (theoretically) run for hours and hours and hours and hours and hours, what keeps this from folding up like a house of cards and crashing the system due to resource exhaustion?

It is not recursion, it is setting a function to be executed again, after the requesting function quits:

1 Like

How so?  The function never 'return’s so the function never formally ends.

Even the author of the article you referenced calls it recursion, as does the Mozilla Developer documentation, Stack Overflow, and everyone else I have read.

Who says it never returns? The method sets up the callback and returns immediately.

1 Like

Damn it all to [cartoon curse-words]!

This is the one thing I hate about web searches: I go all over Hell and Half of Texas looking for an answer, hit all the relevant sites, drink from the various fountains of wisdom, visit the Oracle at Delphi, (etc.), and never find the correct answer - just because I searched for “requestAnimationFrame” instead of Window.requestAnimationFrame", as that is what it is called, and referred to, everywhere I looked.

Now I end up feeling like a complete jerk for not realizing that water is wet before stepping into it.

1 Like

Naw, just feel tired - get rid of the ego. The subject is huge and we just have to keep looking and asking till we understand. A bit tiring, and slows our progress, but who’s on a schedule when it is we that decide the goal.

1 Like

Maybe not ego, but definitely angst.

I already know I’ve signed on for the Labors of Hercules, and that I am Boldly Going where my betters have already been a thousand times before.

I am also hugely over-estimating my programming skills - that’s the ego talking.

But it’s hugely frustrating to want an answer to, (what should be), a simple question, spend over a week searching, and then discover that the key was under the front door mat all the time!

(headbang)

1 Like