这是我的递归函数:
function importTEI(index,data,header){
if (index == data.length){return}
var tei = new dhis2API.trackedEntityInstance();
tei.excelImportPopulator(header,data[index]);
tei.POST(requestCallback,requestCallback,index);
function requestCallback(response){
notificationCallback(response);
setTimeout(function(){
importTEI(response.importStat.index+1,importData,header);
},0);
}
}
函数 importTEI 在函数内使用 setTimeout 调用。在没有 setTimeout 的情况下调用时,在几次请求后会出现此错误 -
Uncaught RangeError: Maximum call stack size exceeded
但是使用 setTimeout 它会永远运行....怎么会呢? setTimeout 内部发生了什么特别的事情?不再是递归调用了吗?
如有任何提示,我们将不胜感激。谢谢。
最佳答案
不再是递归调用。 setTimeout 是将来的回调,该调用将位于“堆栈顶部”。对您的函数的现有调用会设置此回调,然后完成其执行,从而实现零递归。
关于javascript - 为什么 setTimeout 对无休止的递归调用有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161211/