javascript 如何防止由于不需要的递归而导致 IE 中的堆栈溢出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7081073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:54:04  来源:igfitidea点击:

How to prevent a stack overflow in IE due to unwanted recursion

javascriptajaxinternet-explorerstack-overflow

提问by Eldros

I've been searching since a while, to no avail.

我一直在寻找一段时间,无济于事。

On a long form, I'm doing checks with javascript, and used the attribute "onchange" to do them. Recently, I discovered that on IE, the "onpropertychange" attribute should be used, as the "onchange" event is not handled correctly.

在较长的表格中,我正在使用 javascript 进行检查,并使用属性“onchange”来进行检查。最近,我发现在 IE 上,应该使用“onpropertychange”属性,因为“onchange”事件没有正确处理。

So now, I have for example this kind of field (not all the fields have onKeyDown or onKeyUp events):

所以现在,我有这样的字段(并非所有字段都有 onKeyDown 或 onKeyUp 事件):

<textarea name="shdescription" rows="1" cols="75" onKeyDown="textCounter(this,'shdesccounter',65)"
onKeyUp="textCounter(this,'shdesccounter',65)" onchange="checkFormValue();" onpropertychange="checkFormValue();"></textarea>

And now each time I make a change on a field, then it takes a few seconds before registring the change and making the check. (and more when the change is made on the textarea I described.)

现在每次我对一个字段进行更改时,都需要几秒钟的时间才能注册更改并进行检查。(以及在我描述的 textarea 上进行更改时更多。)

Firing the IE F12 debugger, I noticed that a lot of "SCRIPT28: Out of stack space"and "SCRIPT2343: Stack overflow at line:"are fired. Following the execution of the script step by step, I noticed that the script is looping between two different ajax call to PHP scripts. I'm not sure we can talk of recursion actually, as I don't see any direct link between the two function.

启动 IE F12 调试器时,我注意到很多“SCRIPT28:堆栈空间不足”“SCRIPT2343:行堆栈溢出:”被触发。随着脚本的逐步执行,我注意到脚本在对 PHP 脚本的两个不同 ajax 调用之间循环。我不确定我们实际上可以谈论递归,因为我没有看到这两个函数之间有任何直接联系。

So I can think only of two possibilities: either there are change triggered in the field which trigger the the ajax call again, or I am doing something wrong with my ajax calls.

所以我只能想到两种可能性:要么是字段中触发的更改再次触发了ajax调用,要么我的ajax调用有问题。

Where should I look to resolve my problem? If there is need for more information, don't hesitate to ask.

我应该去哪里解决我的问题?如果需要更多信息,请随时询问。

PS: I don't have any problem of this sort with FF/Chrome/Opera.

PS:我对 FF/Chrome/Opera 没有任何此类问题。

采纳答案by m.edmondson

I'll bet that one of the functions checkFormValue()or textCounter()is modifying the textbox in a way that is causing another event to fire which goes on to endlessly loop.

我敢打赌,其中一个函数checkFormValue()textCounter()正在以某种方式修改文本框,导致另一个事件触发,该事件继续无限循环。

Try using onBlur.

尝试使用 onBlur。