1

I have a recursive function in my JavaScript file. It looks something like this:

function process(node){
    if(someCondition)
        return someValue;
    a = process(node.children);
    b = doSomething(a);
    return b;
}

The problem is that I want to display the state of the system to the HTML output on each step of this recursion. There should be a delay between each step. (Just assume that I want to display the recursion live to the users). In any other language I would use a delay() call inside the function, but since JavaScript supports nothing other than setTimeout() to do something like this, I am lost because I don't know how to use the setTimeout call in this particular case.

Normally in simpler cases I would use something like this:

function process(node){
    if(someCondition)
        return someValue;
    setTimeout("process(node.children)", delay);
}

;but since my original function returns a value, I don't know how to proceed.

Thanks in advance.

1
  • 1
    Is there any reason you want to display it live to the users? If you would explain maybe we could change your mind or suggest something different to you. Commented Sep 4, 2010 at 2:40

5 Answers 5

1

I think this will not work as a pure recursive algorithm.

You could store the current value of b in a global variable.

You could then us setInterval to process the value in a similar way, updating the value of b on each iteration, using clearTimeout to stop execution when the condition is met.

Sign up to request clarification or add additional context in comments.

Comments

1
  • Put all the calls into anonymous functions.
  • Put them into a queue.
  • Use a recursive function to apply a 0ms setTimeout on each function.

Example:

var queue = [];
queue.push(function(){callYourCodeThatTakesSomeTime('param1','param2','...')});
var defer = function(queue){
    if (!queue.length)
        return;
    queue.shift()();
    setTimeout(defer, 0, queue);
}
defer(queue);

Comments

0

You might try a web worker, if you're targeting HTML5.

1 Comment

Unfortunately, it looks like even web workers cannot solve this problem. Any ideas?
0

What about that?

var result;

function process(node) {
  if (arguments.length > 1) {
    result = doSomething(result);
    displayResult();
  }
  if (someCondition) {
    result = someValue;
    displayResult();
    return;
  }
  setTimeout(function() {
    process(node.children, true);
  }, delay);
}

Comments

0

You can easily update the dom if you use "safe" recursion, see https://stackoverflow.com/questions/24208676/how-to-use-recursion-in-javascript/24208677

/*
this will obviously crash... and all recursion is at risk of running out of call stack and breaking your page...

function recursion(c){
    c = c || 0;
    console.log(c++);
    recursion(c);
}
recursion();

*/

// add a setTimeout to reset the call stack and it will run "forever" without breaking your page!
// use chrome's heap snapshot tool to prove it to yourself.  :)

function recursion(c){
    setTimeout(function(c){
        c = c || 0;
        console.log(c++);
        recursion(c);
    },0,c);
}

recursion();

// another approach is to use event handlers, but that ultimately uses more code and more resources

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.