【发布时间】:2020-11-11 11:32:26
【问题描述】:
我有一个如下所示的 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 中,它按预期工作。
【问题讨论】:
-
在您的站点上加载任何其他 js 之前包含此脚本就足够了:github.com/turuslan/HackTimer/blob/master/HackTimer.min.js(我在这里找到:stackoverflow.com/questions/5927284/…)
-
明确地说,是桌面 Safari 无法正常工作还是移动 (iPad/iPhone) Safari?
-
@ChrisHaas 其桌面 safari 无法正常运行。
标签: javascript php jquery safari setinterval