Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is continuation-style-programming prone to stack overflow

In response to this question about jQuery effects, I thought about using the callback argument to .fadeIn( 500, my_function ).

While in principle, this is a viable idea, I have no clue (and neither has the jQuery documentation :( ) if the callback is allowed to recurse:

function keep_animating(){
   $("#id").fadeIn(500).fadeOut(500, keep_animating );
}
like image 814
xtofl Avatar asked Feb 18 '11 15:02

xtofl


2 Answers

You could add a debugger breakpoint and test if the stack size increases or not. :)

However, since animations/fadings use setTimeout/setInterval I highly guess that the call depth does not increase, i.e. it's not prone to stack overflows.

like image 184
ThiefMaster Avatar answered Oct 14 '22 19:10

ThiefMaster


I took the time to ask the 'people who know'... There is no stack-overflow, since there is no explicit recursion: the fadeIn, fadeOut ... methods all just create an entry on the effects queue. That means that the keep_animating function is not executed from within the same context.

Courtesy to dave methvin:

What you are describing as "recursion" is not actually recursion. jQuery's visual effects run on a setTimeout timer, so the callback function isn't run immediately as it would be in recursion. Instead, the callback runs after the animation completes in several "steps", each triggered by a setTimeout.

like image 37
xtofl Avatar answered Oct 14 '22 19:10

xtofl