Javascript – Detect Inactivity in Browser Tab

Javascript – Detect Inactivity in Browser Tab

You can detect when a user stops interacting with the page using pure Javascript. Example below is a scrupt that will reload the page when user becomes inactive for 5 seconds. It will still run even though you are in another tab.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
onInactive(5, function () {
    window.location.reload();
});

function onInactive(seconds, cb) {
    var ms = seconds * 1000;
    var wait = setTimeout(cb, ms);

    $(window).bind(‘mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick’, function(){
        clearTimeout(wait);
        wait = setTimeout(cb, ms);
    });
}
</script>

Post Created 130

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top