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

我试图将此块重构为一个函数

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

但是这次给定的数组从头开始重复

我认为问题发生在arguments.callee中 ,所以我可以代替它使用什么?
最好的祝福

只需为您的匿名函数命名 ,以便您可以通过其名称来调用它。

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

函数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