简体   繁体   English

Javascript函数在Chrome中不起作用

[英]Javascript functions not working in chrome

For some reason javascript functions are not working right now. 由于某些原因,javascript函数现在无法正常工作。

function alert(){
alert('Close')
}

When I trigger the alert using: 当我使用以下方式触发警报时:

<span onclick="alert()">Hi</span>

I get the following error: 我收到以下错误:

Uncaught RangeError: Maximum call stack size exceeded 未捕获的RangeError:超出最大调用堆栈大小

What could be wrong? 有什么事吗 Is it something wrong with jQuery? jQuery有什么问题吗?

Example (of the error): http://jsfiddle.net/qNjjN/show 示例(错误): http : //jsfiddle.net/qNjjN/show

You've overridden the default alert() function with your own! 您已经用自己的默认alert()函数覆盖了! So when you call alert() within the method it is making an infinite recursive call. 因此,当您在方法中调用alert() ,它将进行无限次递归调用。

Rename your method to something else and it will be fine :) 将您的方法重命名为其他名称,即可:)

The issue here is that there is a native javascript function called alert and you are essentially overriding it with your alert function. 这里的问题是,有一个本地的javascript函数称为alert而您实际上是用alert函数覆盖它。

When you call your function, your function calls itself over and over. 调用函数时,函数会反复调用自身。 This is called recursion and in your case results in your error. 这称为递归,在您的情况下会导致错误。

You have an infinite loop by calling alert from itself. 您可以通过调用自身的警报来实现无限循环。 This would be fine as a recursive function but there's currently no logic to end itself. 作为递归函数,这很好,但是目前还没有逻辑可以结束。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM