Stop an executing recursive javascript function

Posted by DA on Stack Overflow See other posts from Stack Overflow or by DA
Published on 2010-04-26T20:02:13Z Indexed on 2010/04/26 20:03 UTC
Read the original article Hit count: 242

Filed under:
|

Using jQuery, I've build an image/slide rotator. The basic setup is (in pseudocode):

function setupUpSlide(SlideToStartWith){
    var thisSlide = SlideToStartWith;
    ...set things up...
    fadeInSlide(thisSlide)
}

function fadeInSlide(thisSlide){
    ...fade in this slide...
    fadeOutSlide(thisSlide)
}


function fadeOutSlide(thisSlide){
    ...fade out this slide...
    setupUpSlide(nextSlide)
}

I call the first function and pass in a particular slide index, and then it does its thing calling chain of functions which then, in turn, calls the first function again passing in the next index. This then repeats infinitely (resetting the index when it gets to the last item).

This works just fine.

What I want to do now is allow someone to over-ride the slide show by being able to click on a particular slide number. Therefore, if slide #8 is showing and I click #3, I want the recursion to stop and then call the initial function passing in slide #3, which then, in turn, will start the process again.

But I'm not sure how to go about that. How does one properly 'break' a recursive script. Should I create some sort of global 'watch' variable that if at any time is 'true' will return: false and allow the new function to execute?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about recursion