How to automatically reload a page after a given period of inactivity
If you want to refresh the page if there is no activity then you need to figure out how to define activity. Let's say we refresh the page every minute unless someone presses a key or moves the mouse. This uses jQuery for event binding:
<script>
var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function(e) {
time = new Date().getTime();
});
function refresh() {
if(new Date().getTime() - time >= 60000)
window.location.reload(true);
else
setTimeout(refresh, 10000);
}
setTimeout(refresh, 10000);
</script>
Auto refresh page every 30 seconds
There are multiple solutions for this. If you want the page to be refreshed you actually don't need JavaScript, the browser can do it for you if you add this meta
tag in your head
tag.
<meta http-equiv="refresh" content="30">
The browser will then refresh the page every 30 seconds.
If you really want to do it with JavaScript, then you can refresh the page every 30 seconds with Location.reload()
(docs) inside a setTimeout()
:
window.setTimeout( function() {
window.location.reload();
}, 30000);
If you don't need to refresh the whole page but only a part of it, I guess an AJAX call would be the most efficient way.
How to automatically reload a screen(form) after a given period of inactivity in codenameone
UITimer can be used to schedule tasks.
UITimer timer = new UITimer(new Runnable() {
@Override
public void run() {
...
}
});
timer.schedule(1000, false, Display.getInstance().getCurrent());
asp.net refresh page if user idle or no activity
have to include JQuery library on top, before using that code!
Related Topics
How to Download File from Server
Triggering a JavaScript Click() Event at Specific Coordinates
Chrome Extension: Port Error: Could Not Establish Connection. Receiving End Does Not Exist
Why Were Es5 Object Methods Not Added to Object.Prototype
JavaScript Inheritance: Object.Create VS New
Google Maps API - Getting Closest Points to Zipcode
Create an Instance of a Class in Es6 with a Dynamic Name
Sum of Two Numbers with Prompt
Using Ajax to Read Local Files
Merge Two Arrays So That the Values Alternate
Array.Push() Makes All Elements the Same When Pushing an Object
A Proper Wrapper for Console.Log with Correct Line Number
Typescript Recursive Function Composition
How to Access a Child's State in React