2

My issue is when the user closes the tab then I have to call the ajax to update the logout time in the database. So I tried below code after some research but still, it's not working.

I notice that. It's automatic log out after 20 sec after adding the below script in the JS.

I used below code but this is not a correct code as per suggested by SO users team.

window.onbeforeunload = function(){
  // var msg="Are you sure want to close this tab";
  // return msg;
     $.ajax({
       method:"POST",
       async: false,
       url:baseUrl+"/Employee_control/logoutTimeUpdate"
   });

so I change it to this but still not working.

var _wasPageCleanedUp = false;
function logoutTimeUpdate()
{
    if (!_wasPageCleanedUp)
    {
        $.ajax({
            type: 'GET',
            async: false,
            url:baseUrl+"/Employee_control/logoutTimeUpdate",
            success: function ()
            {
                _wasPageCleanedUp = true;
                alert("hello");
            }
        });
    }
}
$(window).on("unload", function ()
{
    logoutTimeUpdate();
});

Controller

 public function logoutTimeUpdate(){
       $updatedLogoutTime=$this->Employee_model->logoutTimeUpdate();
        if ($updatedLogoutTime == 1) {
         $this->session->unset_userdata('login_session');
         $this->session->sess_destroy();
        } 
    }
}

Model

public function logoutTimeUpdate(){
  $data = array('logout_time' =>$this->current_date);
  $where = array('login_id'=>$this->session->userdata['login_session']['id']);

$this->db->where($where);
$this->db->update('tbl_current_login', $data); 
return 1;

}
questionbank
  • 440
  • 8
  • 25
  • add an event to the window object like this - https://stackoverflow.com/a/19538231/9332875 – Sumit Wadhwa Mar 06 '19 at 06:09
  • @kenzotenma, So which code I have to use in my question? and How do I call ajax using your answer which is in the link? – questionbank Mar 06 '19 at 06:14
  • replace `$(window).on` with the one I shared and place `logoutTimeUpdate();` in between. – Sumit Wadhwa Mar 06 '19 at 06:53
  • I guess the problem your `$(window).on` is not working because it isn't returning anything. for some reason, callbacks for events like `beforeunload` or `unload` need to return something, not sure why. – Sumit Wadhwa Mar 06 '19 at 06:56
  • @kenzotenma, I checked this URL https://stackoverflow.com/questions/4945932/window-onbeforeunload-ajax-request-in-chrome/20322988#20322988. and I tried but not working for me. – questionbank Mar 06 '19 at 07:25
  • I would suggest to use alternate approach if window unload doesn't feels like stable/sustainable to you. I would go with php session check script fired every 10-20 seconds (you can set timing of your own). As i said, this is my version & you may/may not like to go with this approach. – techworld Mar 06 '19 at 15:41
  • @techworld, Thanks for the suggestion, but I can't use that. I have to call ajax when tab close. I don't think so script fired every some sec will be a good idea. – questionbank Mar 06 '19 at 17:40

1 Answers1

0

You can write you code on unload event. For e.g.

<!DOCTYPE html>
<html>
    <head>
       <title></title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
       <div id="ProcessModel">

       </div>
        <script type="text/javascript">
          $(window).on('unload',function () {
                $.ajax({
                    type: 'GET',
                    async: false,
                    url: 'check.php?id=123'
                });
            });
        </script>
    </body>
</html>

this will be call on closing a tab.

bimal sharma
  • 170
  • 8