tags:

views:

35

answers:

2
function runProcess(){
     var todo = items.concat();
     setTimeout(function(){
        process(todo.shift());
        if(todo.length > 0){
           setTimeout(arguments.callee, 25);
        } else {
           callback(items);
        }
     }, 25);
}

I tried to refactor this block into a function

function doWork(todo){
        process(todo.shift());
        if(todo.length > 0){
           setTimeout(arguments.callee, 25);
        } else {
           callback(items);
        }
     }

But this time given array repeats itself from the start

I think the problem occurs in arguments.callee,so what can i use instead of it?
Best Regards

+1  A: 

The function setInterval should meet your needs.

function runProcess(){
     var todo = items.concat(),
         id = setInterval(function(){
                  process(todo.shift());
                  if(todo.length === 0) {
                      clearInterval(id);
                      callback(items);
                  }
              }, 25);
}
ChaosPandion
+1  A: 

Simply give a name to your anonymus function so that you can call it by its name.

function runProcess(){
     var todo = items.concat();
     setTimeout(function step() { // give it a name
        process(todo.shift());
        if(todo.length > 0){
           setTimeout(step, 25);  // call it by its name
        } else {
           callback(items);
        }
     }, 25);
}
galambalazs
This is not a bad alternative but I think it is not supported by all implementations *(Don't quote me...)*.
ChaosPandion
@ChaosPandion: it works, with one side-effect in IE: `"step"` would be introduced into the local scope.
Crescent Fresh
Thank you,one more thing if function step has arguments how can i change ?
Myra
@Myra you mean `setTimeout(function(){ step(arg1, arg2, ar3); }, 25);`? but i see no point since `step` will know the exact same things as `runProcess` because of *closure*.
galambalazs
@ChaosPandion giving a name to a function is not supported in which implementation? It's nonsense. Sorry. :)
galambalazs
@galambalazs It'd be really nice if named functions like that worked all the time, but unfortunately older IE versions do [really weird things](http://kangax.github.com/nfe/#jscript-bugs) when you do that. Even Webkit has bugs.
Pointy
@Pointy - Ah yes, that is what I am remembering.
ChaosPandion
@Pointy it has some bugs, but it doesn't mean it's not supported. In the example above i see no way that would screw up IE. Only **`step`** will be in the **local scope**. All the other cases are **not related** to this situation.
galambalazs
Well, read Kangax's blog post I linked to and then judge for yourself. Personally I don't ever use those because I don't want to deal with the potential headaches. In this case, you could just declare an extra local variable and assign the function to it in the initial call to `setTimeout`, and that'd work just as well.
Pointy
@Pointy I've read it before. I know the cases where you should be careful. This is just not one of them.
galambalazs