简体   繁体   English

具有递归自调用的重写函数,以便解决RangeError最大调用堆栈大小

[英]Rewriting function with recursive self-calls so that RangeError Maximum call stack size is resolved

I have a function that calls itself and I don't see any potential infinite-loops and I am only running the loop if the counter is less than the length of an array. 我有一个可以自我调用的函数,但看不到任何潜在的无限循环,并且仅在计数器小于数组长度的情况下运行循环。

Does anyone have any idea why the call stack size is throwing an error? 有谁知道为什么调用堆栈大小会引发错误?

function train(i, data, n, nTwo, func){
        if(i===data.length && nTwo!==undefined) done();
        else if(netTwo===undefined) func();
        else (new Trainer(n)).workerTrain([data[i]], train(i++,     trainingSet, l, y));
    }
    train(0, trainingSet, l, y);

I can't run this inside a for-loop as I can initiate a worker until the previous one is done. 我无法在for循环中运行此程序,因为在上一个操作完成之前,我可以启动工作程序。 Or at least when I tried to, I got an error. 或者至少当我尝试时,出现错误。 So I'm running the next worker in the previous worker's callback function. 因此,我正在上一个工作程序的回调函数中运行下一个工作程序。

You are using the post-increment operator ( i++ ), which returns the current value before incrementing the variable. 您正在使用后递增运算符( i++ ),该运算符将递增变量之前返回当前值。 In other words, you always call you function with the same i . 换句话说,您总是使用相同的i来调用函数。

Using the pre-incremenet ( ++i ) instead should solve the issue: 使用pre-incremenet( ++i )应该可以解决此问题:

else (new Trainer(n)).workerTrain([data[i]], train(++i, trainingSet, l, y));

netTwo is not defined. netTwo未定义。 nTwo is a parameter in the function and used in the first if clause. nTwo是函数中的参数,并在第一个if子句中使用。 But the else if clause has an undefined variable named "netTwo." 但是else if子句有一个名为“ netTwo”的未定义变量。 I therefore would expect a problem. 因此,我希望有一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript递归函数:未捕获RangeError:超出最大调用堆栈大小 - javascript recursive function: Uncaught RangeError: Maximum call stack size exceeded 递归 Function Uncaught RangeError: Maximum call stack size exceeded - Recursive Function Uncaught RangeError: Maximum call stack size exceeded javascript中递归函数可计数时,最大调用堆栈大小超出RangeError - Maximum call stack size exceeded RangeError when recursive function is countable in javascript 递归新的Promise:RangeError:超出最大调用堆栈大小 - Recursive new Promise: RangeError: Maximum call stack size exceeded 错误:嵌套在自我函数中的“未捕获的范围错误:超出最大调用堆栈大小” - Error:“Uncaught RangeError: Maximum call stack size exceeded” nested in self function RangeError:JS函数中的最大调用堆栈大小错误 - RangeError: Maximum call stack size error in JS function firebase 函数未处理的错误 RangeError:超出最大调用堆栈大小 - firebase function Unhandled error RangeError: Maximum call stack size exceeded 递归函数中超出了最大调用堆栈大小 - Maximum call stack size exceeded in recursive function 递归函数:超出最大调用堆栈大小 - Recursive Function: Maximum call stack size exceeded RangeError:超出最大调用堆栈大小(ParticlesJS 函数) - RangeError: Maximum call stack size exceeded (ParticlesJS function)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM