我正在使用直接Web Remoting (DWR) JavaScript库文件,只在Safari(桌面和iPad)中得到一个错误

它说

超过最大调用堆栈大小。

这个错误到底是什么意思,它是否完全停止处理?

Safari浏览器也有任何修复(实际上是在iPad Safari上,它说

JS:执行超时

我认为这是相同的调用堆栈问题)


当前回答

在我的例子中,我使用以下方法将一个大字节数组转换为字符串:

String.fromCharCode.apply(null, new Uint16Array(bytes))

字节包含数百万个条目,这对于堆栈来说太大了。

其他回答

在我的例子中,我有两个同名的变量!

对我来说 我错误地分配了相同的变量名,并给val函数“class_routine_id”

var class_routine_id = $("#class_routine_id").val(class_routine_id);

应该是这样的:

 var class_routine_id = $("#class_routine_id").val(); 

几乎每个答案都说这只能由无限循环引起。这是不正确的,否则您可以通过深度嵌套调用溢出堆栈(并不是说这是有效的,但它肯定是在可能的范围内)。如果你可以控制你的JavaScript虚拟机,你可以调整堆栈大小。例如:

节点——时= 2000

请参见:如何在Node.js中增加最大调用堆栈大小

在我的例子中,我发送的是输入元素而不是它们的值:

$.post( '',{ registerName: $('#registerName') } )

而不是:

$.post( '',{ registerName: $('#registerName').val() } )

这冻结了我的Chrome标签到一个点,它甚至没有显示我的“等待/杀死”对话框,当页面变得无响应…

这也会导致最大调用堆栈大小超过错误:

var items = [];
[].push.apply(items, new Array(1000000)); //Bad

我也一样:

items.push(...new Array(1000000)); //Bad

来自Mozilla文档:

But beware: in using apply this way, you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.

所以尝试:

var items = [];
var newItems = new Array(1000000);
for(var i = 0; i < newItems.length; i++){
  items.push(newItems[i]);
}