gpt4 book ai didi

javascript - 用户空闲超时只执行一次函数

转载 作者:行者123 更新时间:2023-11-30 11:41:49 24 4
gpt4 key购买 nike

我想在用户空闲超时 5 秒时显示报价横幅,但是以下代码每空闲 5 秒显示一次报价横幅。但是我只想在第一个空闲 5 秒内执行一次以下功能。任何人都可以帮助我

$(document).ready(function(){

idleTime = 0;


//Increment the idle time counter every five second.
var idleInterval = setInterval(timerIncrement, 5000);

function timerIncrement()
{
console.log(idleTime++);
if (idleTime > 5)
{
doPreload();

}
}

//Zero the idle timer on mouse movement.
$(this).mousemove(function(e){

idleTime = 0;

});

function doPreload()
{
$('#add-click').click();

}

});

最佳答案

你只使用鼠标移动,如果用户使用键盘你还必须检查一下怎么办

var inactivityTime = function () {
var temp;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;

function resetTimer() {
clearTimeout(temp);
temp = setTimeout(logout, 3000)
// 1000 milisec = 1 sec
}
};

关于javascript - 用户空闲超时只执行一次函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42366170/

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