Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();

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?

like image 428
jrn Avatar asked Jul 03 '12 14:07

jrn


People also ask

How do you instantly refresh a page?

In most browsers, pressing Ctrl+F5 will force the browser to retrieve the webpage from the server instead of loading it from the cache.

How do you automatically refresh a page after a given period of inactivity?

addEventListener("keydown", checkLastAction); This will reload the page as soon as the user moves their mouse, hits a key or touches a touchscreen if it has been inactive for 1 hour.

How do you refresh a page after 5 seconds?

setTimeout(function(){ window. location. reload(); }, 5000); This example sets 5 seconds to reload the page, you can set the time as per your needs.

How do I refresh a page in Wordpress?

To force a refresh, just navigate to “Tools”, click on “Force Refresh” and click the button that says, “Refresh Site.”


1 Answers

In my development environment I add this snippet of JS to a page - whenever the page becomes active after becoming inactive it reloads, so for example if you type in your text editor then click back onto your browser the page will reload once without an infinite loop. It also works when chaining active tab or active window within the same browser session.

window.onblur= function() {window.onfocus= function () {location.reload(true)}};

As stated above this does depend on browser implementation but works fine in chrome.

like image 111
Eamonn Avatar answered Sep 25 '22 12:09

Eamonn