0

Following is my code that I'm using to logout user after 10 mins of inactivity.

var timer = 0;
    function set_interval() {
        timer = setInterval("auto_logout()", 300000);
    }

    function reset_interval() {

        if (timer != 0) {
            clearInterval(timer);
            timer = 0;
            timer = setInterval("auto_logout()", 300000);
        }
    }

    function auto_logout() {
        window.location = "<?php echo base_url('staff/staff_logout'); ?>";
    }

As for the logging out when browser closes that also has been taken care in the config.php file by doing $config['sess_expiration'] = 0;

But now I want to do it when a tab is closed so I tried the following code:-

var unloadEvent = function (e) {
        var confirmationMessage = "Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?";

        return confirmationMessage;
    };
    window.addEventListener("beforeunload", unloadEvent);

But it's not working. I'm using Chrome 71.0.3578.98. Any help will be appreciated. thanks in advance.

Alek Stephanok
  • 193
  • 2
  • 17
  • You don't show the confirmation message with `window.confirm` – yunzen Jan 09 '19 at 14:02
  • @HerrSerker I actually want the user to be logged out if he/she closes the tab so for that I have used beforeunload but it's not happening in chrome. can give me an example?? – Alek Stephanok Jan 09 '19 at 14:07
  • [beforeunload](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload) event should show a confirmation message when page is unloaded. Alas this fires when you click on a link and not only, when you close the tab – yunzen Jan 09 '19 at 14:12
  • You cannot destinguish between a tab close and a follow link event: https://stackoverflow.com/a/3888938/476951 – yunzen Jan 09 '19 at 14:14
  • You may want to look at https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload though – yunzen Jan 09 '19 at 14:20
  • This may be a solution https://stackoverflow.com/a/44057659/476951 – yunzen Jan 09 '19 at 14:22

0 Answers0