我正在开发一个用户可以登录的应用程序。我在数据库中添加登录时间并创建一个login_session
。
我的问题是当用户关闭选项卡然后我必须调用ajax来更新数据库中的注销时间。所以我经过一些研究后尝试了下面的代码,但仍然没有用。
我在js文件中添加了以下脚本
var unloadHandler = function(e){
$.ajax({
method:"POST",
url:baseUrl+"/Employee_control/logoutTimeUpdate";
});
};
window.unload = unloadHandler;
调节器
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;
}
我试过JS但是当我关闭标签时它并没有调用我的AJAX。我试着直接测试URL www.example.com/Employee_control/logoutTimeUpdate
并且它正在工作,但我不知道当我关闭标签时它不起作用
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"
});
}
你能帮我解决这个问题吗?
发布于 2019-03-04 14:56:19
你应该做两件事:
async: false
ajax调用中的设置,以便在执行ajax之前不会关闭选项卡。unload
事件,你应该使用onbeforeunload
。第一个因引发AJAX调用问题而闻名,可以在这里阅读。基本上,请尝试以下方法:
var unloadHandler = function(e){
$.ajax({
method:"POST",
async: false,
url:baseUrl+"/Employee_control/logoutTimeUpdate";
});
};
window.onbeforeunload = unloadHandler;
https://stackoverflow.com/questions/-100006420
复制相似问题