I want to use recursion in JavaScript to produce an infinite loop. In fact, I desire to give an image a come-and-go effect, endlessly.
Let's take a look at some code first :
function lightening(){
$('#pic_holder').fadeOut(250).fadeIn(250);
setTimeout('lightening', 250);
}
This function, as it's written, should
- apply the
fadeOut(250)andfadeIn(250)effects ; - engage the
setTimeoutfunction which in its turn must call recursively thelighteningfunction, henceforth re-applying the [fadeOut-fadeIn effect and setTimeout] block of code.
This, you'll agree, should go ad infinitum, but it doesn't.
Here's the full test code, with HTML, as you can notice, it applies the fadeOut-fadeIn effect only once.
What am I doing wrong ?
Recursion in computer programming is exemplified when a function is defined in terms of simpler, often smaller versions of itself. The solution to the problem is then devised by combining the solutions obtained from the simpler versions of the problem.The main disadvantage is often that the algorithm may require large amounts of memory if the depth of the recursion is very large.. This seems to imply a stack (I'm ignoring TCO here), where each invocation returns state. This is just emulatingsetInterval, which I hope we can agree is not recursion.