简体   繁体   English

发生错误后,有没有办法“重新初始化”JavaScript?

[英]Is there a way to “re-initialize” JavaScript after an error occurred?

We are currently using the following code to track javascript errors on our website:我们目前正在使用以下代码来跟踪我们网站上的 javascript 错误:

window.onerror = function(message) {
    log(message);
};

The log-function in the code above displays the error message within the console for developers but not for regular users.上面代码中的日志功能在控制台中为开发人员显示错误消息,但对普通用户不显示。 We are thinking about displaying some kind of information for regular users and then to reload the page after the user confirms the message.我们正在考虑为普通用户显示某种信息,然后在用户确认消息后重新加载页面。 It would however be better if we could figure out a way to make sure that javascript continues to work even after an error has occurred.然而,如果我们能找到一种方法来确保 javascript 即使在发生错误后也能继续工作,那就更好了。 So we are wondering if there is a way to "re-initialize" javascript to start over after an error has occurred.所以我们想知道是否有办法“重新初始化” javascript 在发生错误后重新开始。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

Sorry if you've already tried this, but did you try a "try catch" block?抱歉,如果您已经尝试过,但是您是否尝试过“try catch”块?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

This will allow it to encounter an error, and then move on.这将允许它遇到错误,然后继续。 In the "catch" block you could do something with error, but you can also just run other code as well after the error has occurred.在“catch”块中,你可以做一些错误的事情,但你也可以在错误发生后运行其他代码。

There is no built-in way to re-initialize your code (other than reloading the page), but here is a way you could do it:没有内置方法可以重新初始化您的代码(除了重新加载页面),但您可以使用以下方法:

Save a copy of the object key names in the window object.window object 中保存 object 键名的副本。 Wrap your starting code in a function and call the function once on startup.将您的起始代码包装在 function 中,并在启动时调用 function 一次。 If you ever detect an error, delete all key-value pairs from the window object that were not saved in the original copy;如果您检测到错误,请从window object 中删除所有未保存在原始副本中的键值对; this will delete all global vars.这将删除所有全局变量。 Finally, call your initialize function again.最后,再次调用您的初始化 function。

This may have some complications when implementing, but should work most of the time.这在实施时可能会有一些复杂性,但在大多数情况下应该可以工作。

As @That'sIs JustCrazy said, left over event listeners may be a problem, so you will also need a way to reset the DOM.正如@That'sIs JustCrazy 所说,遗留的事件侦听器可能是一个问题,因此您还需要一种重置 DOM 的方法。 You can save a copy of the original contents of your html tag at the beginning, const savedHTML = document.getElementsByTagName("html")[0].innerHTML .您可以在开头保存html标记的原始内容的副本, const savedHTML = document.getElementsByTagName("html")[0].innerHTML Then run document.getElementsByTagName("html")[0].innerHTML = savedHTML .然后运行document.getElementsByTagName("html")[0].innerHTML = savedHTML I do remember reading somewhere that deleting elements with event listeners will cause memory leaks on IE, but on other browsers it is fine.我记得在某处读过,删除带有事件侦听器的元素会导致 memory 在 IE 上泄漏,但在其他浏览器上没问题。

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

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