我有一个 JS/jQuery 代码,如下所示,其中我希望在会话选项卡不活动时保持 JS/jQuery 代码正常工作。
以下代码在 Google Chrome 中完美运行,但在 Safari 中不起作用。
jQuery(document).ready(function ($) {
let lastActivity = <?php echo time(); ?>; // Line A
let now = <?php echo time(); ?>;
let logoutAfter = 3600; // page will logout after 1800 seconds if there is no activity
let userName = "<?php echo $_SESSION['user_name']; ?>";
let timer = setInterval(function () {
now++;
let delta = now - lastActivity;
console.log(delta); // Line A
if (delta > logoutAfter) {
clearInterval(timer);
//DO AJAX REQUEST TO close.php
$.ajax({
url: "/control/admin.php",
type: 'GET', // GET also fine
data: {action: 'logout', user_name: userName},
success: function (data) {
window.location.href = "admin.php";
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
}
}, 1000); //<-- you can increase it( till <= logoutAfter ) for better performance as suggested by @"Space Coding"
});
Line A当选项卡未处于活动状态时,Safari 中的值不会增加,但在 Google Chrome 中却可以正常工作。在 Google Chrome 中,它按预期工作。
缥缈止盈
慕斯王
潇潇雨雨