gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:37:55 24 4
gpt4 key购买 nike

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

我试图将这个 block 重构为一个函数

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

关于javascript - 我还能用什么来代替 arguments.callee?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3874922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com