簡體   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