如何在焦点后刷新页面? [英] How to refresh page after focus?
问题描述
我想重新回到焦点后刷新页面。对于我认为我可能会使用的更新部分:
I would like to refresh a page after it got back into the focus. For the refreshing part I thought I might just use:
location.reload();
棘手的部分是如何确定何时刷新?
首先窗口必须松开焦点(可以通过 .blur()确定)。
只有在失去焦点的窗口重新聚焦后才会触发刷新本身。我可以在位置使用 .focus()吗?或者我必须在这里使用其他东西吗?
The tricky part is how do I determine when to refresh?
First the window has to loose the focus (which could be determined through .blur() I assume).
Only after the window which lost focus gets back into focus should trigger the refresh of itself. Can I use .focus() on location? Or do I have to use something else here?
我如何结合这两种情况?
How can I combine these two cases?
什么会你的做法是什么?
What would your approach be?
推荐答案
试试这个:
var blurred = false;
window.onblur = function() { blurred = true; };
window.onfocus = function() { blurred && (location.reload()); };
这只会在隐藏页面时重新加载页面
This only reloads the page if it has been hidden
我不建议在jQuery中绑定窗口焦点,Page会重新加载几乎所有用户操作。
I don't recommend binding to the window focus in jQuery, the Page will reload on pretty much every user action.
更新
这很大程度上依赖于window.onfocus和window.onblur的浏览器实现。 Webkit可能不支持onblur事件(在我的chrome中没有),如果页面被隐藏,firefox会调用模糊的颜色。
This heavily relies on the browser implementation of window.onfocus and window.onblur. Webkit may not support the onblur event (in my chromium it didn't) where firefox calls a blur if the page is hidden.
这篇关于如何在焦点后刷新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!