简体   繁体   English

如何执行“元刷新”,但仅在页面存在的情况下

[英]How to do “meta refresh”, but only if the page exists

I have a simple SPA used by our team, using angular1. 我有一个简单的SPA,供我们的团队使用,使用angular1。 It gets most of its data from REST calls, but I also have a "meta refresh" tag to occasionally reload the whole page, to make sure the user gets occasional small functionality changes. 它从REST调用中获取大部分数据,但我还有一个“元刷新”标签,偶尔会重新加载整个页面,以确保用户偶尔会进行小的功能更改。 There is no need to ever make this optional, it should just reload the page. 无需将此选项设为可选,只需重新加载页面即可。

The minor annoyance with this strategy is that when the user sometimes disconnects from the network (traveling between work and home, for instance), the page reload fails, and it gets into an error state. 这种策略的一个小麻烦是,当用户有时断开网络连接(例如,在工作和家庭之间旅行)时,页面重新加载失败,并且进入错误状态。 It's easy enough to just force reload the page, but I'd like for this to be a little cleaner, such that it only reloads the page if it can reach the page. 强制重新加载页面很容易,但是我希望这样做更加干净,以便仅在可以到达页面时才重新加载页面。

I noticed the advice at https://davidwalsh.name/meta-refresh-javascript . 我在https://davidwalsh.name/meta-refresh-javascript上注意到了建议。 That uses javascript to do the reload, but it's not conditional on the page existing. 它使用javascript进行重新加载,但它不以现有页面为条件。

Is there a reasonable variation of this that will simply skip the reload when it can't reach the page, and then reload again when it can? 是否有合理的变化,可以在无法到达页面时跳过重新加载,而在无法到达页面时再次重新加载?

The navigator object has a property online that you could check navigator对象具有一个online属性,您可以检查该属性

if(window.navigator.online){
  window.location.reload(); 
}

Or make an ajax head request first and if that succeeds do the refresh 或者先发出ajax head请求,如果成功,则执行刷新

For my needs, I ended up implementing a "heartbeat" method in my controller, which calls a Heartbeat service, which does a HEAD request to my REST service, and returns true if the status is 200. 出于我的需要,我最终在控制器中实现了“心跳”方法,该方法调用心跳服务,该服务对REST服务执行HEAD请求,如果状态为200,则返回true。

I then call this from the HTML like this: 然后,我从HTML中这样调用它:

<script type="text/javascript">
    window.setInterval(function() {
        // Call controller method to call heartbeat service.
        if (angular.element(document.body).scope().heartbeat()) {
            window.location.reload();
        }
    }, 900000)
</script>

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

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