这是我的递归函数:
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);
}
}
使用setTimeout在函数内调用函数importTEI。如果在没有setTimeout的情况下调用,则在几次请求后会出现此错误-
Uncaught RangeError: Maximum call stack size exceeded
但是有了setTimeout,它就可以永远运行。setTimeout内部发生了什么特别的事情?它不再是递归调用了吗?
任何提示,不胜感激。谢谢
它不再是递归调用。setTimeout是将来的回调,并且该调用将在“堆栈顶部”。对函数的现有调用将设置此回调,然后完成其执行,从而使递归为零。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句