0
我使用onbeforeunload通過Ajax請求重置變量。但它只執行一次。例如,如果我訪問該頁面(登錄後)並關閉該窗口,則會執行該功能,但如果我重複該操作(將地址放入瀏覽器並完成登錄並關閉窗口),則不會執行該功能。onbeforeunload僅執行一次
window.onbeforeunload = function (e) {
//Ajax request to reset variable in database
resetDemoValueWithAjax();
};
//Ajax request to reset variable in database and exit
function resetDemoValueWithAjax(){
$.ajax({
async:true,
type: "POST",
dataType: "json",
url:"http://localhost/site/index.php/welcome/resetDemoValue",
data: {name: "demo"},
success:function(){
document.location.href='http://localhost/site/index.php/auth/logout';
},
error: function(){
document.location.href='http://localhost/site/index.php/auth/logout';
}
});
}
document.location.href='http://localhost/site/index.php/auth/logout';
僅在另一時刻使用:當用戶註銷時。不在這裏
清晰簡潔。謝謝。 – vicenrele