How can I automatically reload a webpage, if there have been no activity on the page for a given period of time?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- void before promise syntax
- Keeping track of variable instances
Above will refresh the page every 10 minutes unless resetTimeout() is called. For example:
Based on the accepted answer of arturnt. This is a slightly optimized version, but does essentially the same thing:
Only difference is that this version uses
setInterval
instead ofsetTimeout
, which makes the code more compact.I would consider
activity
to be whether or not the user is focused on the window. For example, when you click from one window to another (e.g. Google Chrome to iTunes, or Tab 1 to Tab 2 within an internet browser), the webpage can send a callback saying "Im out of focus!" or "Im in focus!". One could use jQuery to harness this possible lack of activity to do whatever they wanted. If I were in your position, I would use the following code to check for focus every 5 seconds, etc and reload if no focus.This can be accomplished without javascript, with this metatag:
where content ="5" are the seconds that the page will wait until refreshed.
But you said only if there was no activity, what kind for activity would that be?
And finally the most simple solution:
With alert confirmation:
Without alert confirmation:
Body code is the SAME for both solutions