How to Automatically Reload a Page After a Given Period of Inactivity

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



Leave a reply



Submit