简体   繁体   English

我还能用什么代替arguments.callee?

[英]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? 我认为问题发生在arguments.callee中 ,所以我可以代替它使用什么?
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. 函数setInterval应该可以满足您的需求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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