Myra
Myra

Reputation: 3656

What else can I use instead of arguments.callee?

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

Upvotes: 1

Views: 913

Answers (2)

gblazex
gblazex

Reputation: 50109

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);
}

Upvotes: 2

ChaosPandion
ChaosPandion

Reputation: 78272

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);
}

Upvotes: 1

Related Questions