gpt4 book ai didi

javascript - 如果用户处于非事件状态,则运行 jQuery 超时函数

转载 作者:行者123 更新时间:2023-11-30 17:14:15 28 4
gpt4 key购买 nike

我有一个 jQuery 超时脚本,它会在 30 分钟后运行,但是,它一直在运行,我只希望它在用户在这段时间内处于非事件状态时运行。我该怎么做?

$(function(){
var timeout = 30000;

$(document).on("mousemove", function(){
clearTimeout(timeout);
})
setTimeout(function(){
$.post("../php/logout.php", {}, function(response){
if(response.success == "1"){
location.replace("../pages/timed_out.php");
}
}, "json");
}, timeout);
})

最佳答案

您应该通过在 clearTimeout 中使用超时 ID(可以作为 setTimeout 函数结果获得)清除超时并再次设置超时来重置超时:

$(function()
{
var timeout = 30000;
var timer = 0;

setTimer();

$(document).on("mousemove", function()
{
clearTimeout(timer);
setTimer();
});

function setTimer()
{
timer = setTimeout(function()
{
$.post("../php/logout.php", {}, function(response)
{
if (response.success == "1")
{
location.replace("../pages/timed_out.php");
}
}, "json");
}, timeout);
}
});

关于javascript - 如果用户处于非事件状态,则运行 jQuery 超时函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26412769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com