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?
In most browsers, pressing Ctrl+F5 will force the browser to retrieve the webpage from the server instead of loading it from the cache.
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.
setTimeout(function(){ window. location. reload(); }, 5000); This example sets 5 seconds to reload the page, you can set the time as per your needs.
To force a refresh, just navigate to “Tools”, click on “Force Refresh” and click the button that says, “Refresh Site.”
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With