Javascript 检查用户是否处于非活动状态。如果用户单击“继续”按钮,则显示弹出窗口并重置

Javascript 检查用户是否处于非活动状态。如果用户单击“继续”按钮,则显示弹出窗口并重置,javascript,jquery,Javascript,Jquery,要求: 如果用户处于非活动状态,则在5分钟后显示弹出窗口。如果选择继续会话,计时器将为此重置,并再次检查相同的设置 如果用户尚未单击任何“继续”按钮,则页面将刷新 如何检查它们是否处于非活动状态 您可以将事件绑定到键盘和鼠标,并在每次鼠标移动/单击或按键时重置计时器。 <body onmousemove = "canceltimer()"; onclick = "canceltimer()"> var tim = 0; function reload () { tim = se

要求:

  • 如果用户处于非活动状态,则在5分钟后显示弹出窗口。如果选择继续会话,计时器将为此重置,并再次检查相同的设置

  • 如果用户尚未单击任何“继续”按钮,则页面将刷新


  • 如何检查它们是否处于非活动状态

    您可以将事件绑定到键盘和鼠标,并在每次鼠标移动/单击或按键时重置计时器。

    
    
    <body onmousemove = "canceltimer()"; onclick = "canceltimer()">
    
    var tim = 0; 
    function reload () 
    {
    tim = setTimeout("location.reload(true);",180000);   // 3 minutes
    }
    function canceltimer() 
    {
     window.clearTimeout(tim);  // cancel the timer on each mousemove/click
     reload();  // and restart it
     }
    
    var-tim=0; 函数重载() { tim=setTimeout(“location.reload(true);”,180000);//3分钟 } 函数canceltimer() { window.clearTimeout(tim);//取消每次鼠标移动/单击时的计时器 重载();//并重新启动它 }
    使用jQuery:

    $(function() {
            (function handleInactivity() {
                var maxIdleTime = 5000; // 5 seconds
                var timeout = setTimeout(displayPopup, maxIdleTime);
    
                function resetTimer() {
                    console.log("Timer reset");
                    clearTimeout(timeout);
                    timeout = setTimeout(displayPopup, maxIdleTime);
                }
    
                function displayPopup() {
                    console.log("You're up");
                    // Display popup of your choice
                }
    
                $(document).on("mousemove", resetTimer);
                $(document).on("mouseDown", resetTimer);
                $(document).keypress(resetTimer);
            })();
        });
    

    谢谢你的回答,我已经按照我的要求改进了
     _inactiveUserPopUp = function(warningTime,PageReloadTimeAfterWaring){
    
            var maxIdleTime = warningTime *1000,timeout,resetTimer,displayPopup,pageReload;
    
                timeout = setTimeout(displayPopup, maxIdleTime);
    
                resetTimer = function(){
                 // console.log("Timer reset");
                clearTimeout(timeout);
                timeout = setTimeout(displayPopup, maxIdleTime);
            };
    
                displayPopup = function(){
                    //console.log("You're up");
                clearTimeout(timeout);
                var reloadPage = setTimeout(pageReload, PageReloadTimeAfterWaring*1000);
    
                $(".modalDialog").css({
                    "opacity": 1,
                    "display": "block"
                });
    
                $(".close").on("click", function() {
                    $(".modalDialog").css({
                        "opacity": 0,
                        "display": "none"
                    });
                });
    
                $("#extend-session").off().on("click", function() {
    
                    clearTimeout(reloadPage);
    
                    $(".modalDialog").css({
                        "opacity": 0,
                        "display": "none"
                    });
    
                    $.ajax({
                        url: $("#openModal").data("ajaxUrl"),
                        type: "POST",
                        data: {
                            activeUser: true
                        },
                        success: function(data) {
    
                        }
                    });
    
                });
            };
    
            pageReload = function(){
                  //console.log("reload page now")
                $(document).off("mousemove");
                $(document).off("mouseDown");
                $(document).off("keypress");
    
                window.location.reload();
            };
    
            $(document).on("mousemove", resetTimer);
            $(document).on("mouseDown", resetTimer);
            $(document).keypress(resetTimer);
    
        };