javascript - 同步超时问题(Javascript 和 ajax)
问题描述
我正在制作一个超时系统,在一定时间后注销使用,用户打开标签浏览器。我不想在用户使用系统时取代他。为此,我正在检查他何时移动鼠标并重置时间。问题发生在那里,如果它在一个选项卡中,那就完美了。但是,当在 2 个选项卡步骤中,会话结束并且不会向用户显示消息。**本地存储**更改正确,但在另一个选项卡上,它继续运行。我想,当本地存储为 0 时,强制 ajax 结束会话。** 跟随代码 **
SetTimeOut:
function () {
//1800000
var tempo = 10000;
localStorage.setItem("tempo", "1");
var timeout= 0;
var x = new Boolean(false);
function EventoAlert() {
swal({
title: "Sessão expirada",
text: "Sessão expirada por inatividade",
icon: "dist/img/time-clock.webp",
button: true,
button: "Sair",
className: "swalsair",
closeOnClickOutside: false,
}).then((value) => {
var url_ajax = 'index.php';
window.location.href = url_ajax;
}).catch(swal.noop);
};
$(function () {
timeout = setTimeout(function () { }, tempo);
});
$.ajax({
type: 'POST',
url: 'index.php?id=98',
data: '236464302',
success:
function () {
$(document).on('mousemove', async function () {
if(localStorage.getItem("tempo") == "0" && !x){
$.ajax({
type: 'POST',
url: 'index.php?id=59&func=timeout',
data: '236464302',
success: function () {
EventoAlert();
},
error: function () {
window.location.href = 'index.php';
}
});
}
if (timeout !== null) {
clearTimeout(timeout);
timeout = setTimeout(function () { }, tempo);
}
console.log("testeeeeeee");
timeout = setTimeout(function () {
$.ajax({
type: 'POST',
url: 'index.php?id=59&func=timeout',
data: '236464302',
success: function () {
localStorage.setItem("tempo", "0");
x = true;
EventoAlert();
},
error: function () {
window.location.href = 'index.php';
}
});
}, tempo);
});
},
error: function () {
window.location.href = 'index.php';
}
});
},
解决方案
要检查用户何时移动鼠标,您需要在代码中输入 ctrl + c :
var secondsUntilStop = [your time here];
var s;
window.onload = function(){
setInterval(check, 1000);
}
function check(){
s++
if(s >= secondsUntilStop){
//your program stop code here
}
}
推荐阅读
- python - 我无法在 iplot 中的 python 中使用交互
- sql - 分组中的部分总和?
- apache-kafka - 使用kafka-python消费kafka,seek()重置的本地偏移量会提交给kafka吗?
- python - 为什么随着训练的进行保存 state_dict 会变慢?
- regex - 批处理脚本中的正则表达式
- angular - 为什么 npm-ci 修复了我们的 TypeErrors、Assertion Error 等?
- python - 基于特定未经身份验证用户的 ip 地址的 django 油门速率限制
- rust - 如何为 gtk 调用创建“if-let”块?
- android - 应用程序关闭时显示灰色圆圈作为通知图标
- discord - 使用 Discord Bot 显示 Youtube 视频