如果出现JavaScript错误,则刷新页面 [英] Page refresh in case of javascript errors

查看:243
本文介绍了如果出现JavaScript错误,则刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些运行javascript的页面,用于显示不同度量值的更新.这些东西似乎运行良好.但是-始终存在JavaScript在发生错误的情况下可能崩溃的风险(尤其是在运行数小时后)

I have some pages running javascript for displaying updates of different measure values. These stuff seems to run fine. But - there is always the risk that javascript can crash in case of a fault (especially after running some hours)

所以我想实现一种简单的看门狗.我的想法之一是使用meta-refresh-tag.浏览器将在xy分钟后重新加载该网站,并且所有javascript都将重新启动.

So I want to implement a simple kind of watch dog. One of my ideas is to use a meta-refresh-tag. The Browser will reload the site after xy minutes and all javascripts will be reinitalized.

如果没有发生错误,我当然不想刷新站点,并且想使用javascript重置刷新计时器.只要js长时间运行,计时器就会定期重置.如果javascript崩溃,则meta-refresh-timer倒计时为零,并且页面将重新加载.

Of course I do not want to refresh the site if no error occured and want to reset the refresh timer using javascript. As long js runs the timer will be resetted periodally. If javascript crashs the meta-refresh-timer counts down to zero and the page will be reloaded.

阅读stackoverflow帖子,我发现一些答案说无法通过javascript重置元计时器.您知道实现看门狗的更好方法吗?自行使用javascript刷新是没有用的:如果脚本崩溃,它将永远不会触发重新加载事件.

Reading stackoverflow postings I have found some answers that says a reset of the meta-timer by javascript is not possible. Do you know a better way to implement the watchdog? Using javascript ittself for refreshing is useless: If the script crashs it will never fire a reload event..

推荐答案

理想情况下,您应该正确地处理错误,而不仅仅是重新加载页面.但是,您可以像这样使用window.onerror事件:

Ideally you should properly handle the error instead of just reloading the page. However you could use the window.onerror event like this:

window.onerror = function() {
    location.reload();
}

此处有关onerror的更多信息:

https://developer.mozilla.org/en/docs/DOM/window.onerror

这篇关于如果出现JavaScript错误,则刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆