简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM