关闭选项卡后,更新数据库中的注销时间

问题描述 投票:3回答:1

我的问题是当用户关闭选项卡然后我必须调用ajax来更新数据库中的注销时间。所以我经过一些研究后尝试了下面的代码,但仍然没有用。

我注意到了。在JS中添加以下脚本后,它会在20秒后自动注销。

我使用下面的代码,但根据SO用户团队的建议,这不是正确的代码。

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"
   });

所以我把它改成了这个但仍然没有用。

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();
});

调节器

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

模型

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;

}
php jquery mysql ajax codeigniter-3
1个回答
0
投票

您可以在卸载事件上编写代码。对于例如

<!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>

这将在关闭标签时调用。

© www.soinside.com 2019 - 2024. All rights reserved.