1
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

Myra
  • 3,646
  • 3
  • 38
  • 47

2 Answers2

2

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);
}
gblazex
  • 49,155
  • 12
  • 98
  • 91
  • This is not a bad alternative but I think it is not supported by all implementations *(Don't quote me...)*. – ChaosPandion Oct 06 '10 at 17:03
  • @ChaosPandion: it works, with one side-effect in IE: `"step"` would be introduced into the local scope. – Crescent Fresh Oct 06 '10 at 17:05
  • Thank you,one more thing if function step has arguments how can i change ? – Myra Oct 06 '10 at 17:05
  • @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*. – gblazex Oct 06 '10 at 17:07
  • @ChaosPandion giving a name to a function is not supported in which implementation? It's nonsense. Sorry. :) – gblazex Oct 06 '10 at 17:10
  • @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 Oct 06 '10 at 17:11
  • @Pointy - Ah yes, that is what I am remembering. – ChaosPandion Oct 06 '10 at 17:14
  • @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. – gblazex Oct 06 '10 at 17:15
  • 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 Oct 06 '10 at 17:19
  • @Pointy I've read it before. I know the cases where you should be careful. This is just not one of them. – gblazex Oct 06 '10 at 17:22
1

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
  • 77,506
  • 18
  • 119
  • 157