jquery JS会话时间不工作,如果标签不是我的网站页面的焦点

ve7v8dk2  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(125)

当我的标签没有聚焦时,会话计时器停止。我希望它在我的网站在浏览器中打开时运行。任何解决方案?

  1. function startTimer() {
  2. timeLimitInSeconds--;
  3. var minutes = Math.floor(timeLimitInSeconds / 60);
  4. var seconds = timeLimitInSeconds % 60;
  5. if (minutes == 1 && seconds == 0) {
  6. Swal.fire({
  7. title: 'Your session will logout within 1 minute. Do you want to continue?',
  8. // text: "You won't be able to revert this!",
  9. icon: 'warning',
  10. showCancelButton: true,
  11. confirmButtonColor: '#3085d6',
  12. cancelButtonColor: '#d33',
  13. confirmButtonText: 'Yes ',
  14. cancelButtonText: 'No',
  15. allowOutsideClick: false,
  16. allowEscapeKey: false
  17. }).then((result) => {
  18. if (result.isConfirmed) {
  19. console.log("Session Continue Windows Reload..")
  20. window.location.reload()
  21. } else {
  22. window.location = "${sessionScope.clienturl}/logout";
  23. }
  24. })
  25. } else if (minutes <= 0 && seconds <= 0) {
  26. console.log("Logout Fired After Time Out")
  27. window.location = "/logout";
  28. }
  29. if (timeLimitInSeconds < 0) {
  30. timerElement.textContent = '00:00';
  31. clearInterval(timerInterval);
  32. return;
  33. }
  34. if (minutes < 10) {
  35. minutes = '0' + minutes;
  36. }
  37. if (seconds < 10) {
  38. seconds = '0' + seconds;
  39. }
  40. timerElement.textContent = minutes + ':' + seconds;
  41. }

字符串

3npbholx

3npbholx1#

这个问题可能源于浏览器处理计时器和时间间隔的方式,当一个选项卡不在焦点上时。浏览器通常会限制或暂停计时器,以节省非活动选项卡的资源。如果您依赖客户端JavaScript计时器,此行为可能会影响您的会话管理。
以下是一些你可以考虑的方法来处理这种情况:
使用服务器端会话管理:考虑在服务器端管理会话。这种方法更可靠,因为它不依赖于客户端JavaScript。当用户与您的网站交互时,向服务器发送请求以更新会话状态。我希望这对您有所帮助

相关问题