Javascript自动注销代码

时间:2014-04-11 22:57:30

标签: javascript

我有以下代码,大部分都有效,但我想知道是否有可能稍微调整一下。所以下面的代码所做的是,如果它们没有x个毫秒的鼠标活动,则会显示一个弹出窗口,说明它们将被注销,然后当/如果你最终单击ok按钮时,脚本将自动引导你注销文件。

然而,我想要做的是,如果在x毫秒之后没有点击确定按钮,那么继续将屏幕带到logout.php文件。任何人都知道如何使用以下代码执行此操作? 感谢

// Set timeout variables.
var timoutWarning = 840000; // Display warning in 14 Mins.
var timoutNow = 100000; // Timeout in 15 mins would be 900000.
var logoutUrl = 'logout.php'; // URL to logout page.

var warningTimer;
var timeoutTimer;

// Start timers.
function StartTimers() {
    warningTimer = setTimeout("IdleWarning()", timoutWarning);
    timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}

// Reset timers.
function ResetTimers() {
    clearTimeout(warningTimer);
    clearTimeout(timeoutTimer);
    StartTimers();
    $("#timeout").dialog('close');
}

// Show idle timeout warning dialog.
function IdleWarning() {
//  $("#timeout").dialog({
    //modal: true
    alert("Warning, your page will redirected to login page. Due to not move your mouse within the page in 15 minutes.");
//});
}

// Logout the user.
function IdleTimeout() {
    window.location = logoutUrl;
}

5 个答案:

答案 0 :(得分:15)

从概念上讲,您一次只需要运行1个计时器。一个计时器运行14分钟,另一个计时器运行另一分钟(总计15分钟)。一旦14分钟计时器用完,将其杀死然后启动1分钟计时器。如果该一分钟计时器用完,请将用户注销。如果用户按下“保持登录状态”按钮,则终止1分钟计时器并重新启动14分钟计时器。冲洗并重复。

我尽我所能改变了你的代码。希望你明白这一点。

// Set timeout variables.
var timoutWarning = 840000; // Display warning in 14 Mins.
var timoutNow = 60000; // Warning has been shown, give the user 1 minute to interact
var logoutUrl = 'logout.php'; // URL to logout page.

var warningTimer;
var timeoutTimer;

// Start warning timer.
function StartWarningTimer() {
    warningTimer = setTimeout("IdleWarning()", timoutWarning);
}

// Reset timers.
function ResetTimeOutTimer() {
    clearTimeout(timeoutTimer);
    StartWarningTimer();
    $("#timeout").dialog('close');
}

// Show idle timeout warning dialog.
function IdleWarning() {
    clearTimeout(warningTimer);
    timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
    $("#timeout").dialog({
        modal: true
    });
    // Add code in the #timeout element to call ResetTimeOutTimer() if
    // the "Stay Logged In" button is clicked
}

// Logout the user.
function IdleTimeout() {
    window.location = logoutUrl;
}

答案 1 :(得分:1)

 <script type="text/javascript">
             var IDLE_TIMEOUT = 10; //seconds
                var _idleSecondsCounter = 0;
                document.onclick = function() {
                _idleSecondsCounter = 0;
                };
                document.onmousemove = function() {
                _idleSecondsCounter = 0;
                };
                document.onkeypress = function() {
                _idleSecondsCounter = 0;
                };
                window.setInterval(CheckIdleTime, 1000);

                function CheckIdleTime() {
                _idleSecondsCounter++;
                var oPanel = document.getElementById("SecondsUntilExpire");
                if (oPanel)
                oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
                if (_idleSecondsCounter >= IDLE_TIMEOUT) {
                //alert("Time expired!");
                document.location.href = "logout.php";
                }
                }
        </script>

答案 2 :(得分:0)

我必须为我们的项目执行相同的功能。 使用以下代码: -

 <script>
   $(document).click(function(){
        if(typeof timeOutObj != "undefined") {
            clearTimeout(timeOutObj);
        }

        timeOutObj = setTimeout(function(){ 
            localStorage.clear();
            window.location = "/";
        }, 1200000);   //will expire after twenty minutes

   });
</script>

以上代码每次点击屏幕上的任意位置时都会设置一个计时器。 万一我们不点击它会自动退出到主屏幕。

答案 3 :(得分:0)

更新为@VtoCorleone的答案:

var warningTimeout = 840000;
var timoutNow = 60000;
var warningTimerID,timeoutTimerID;

function startTimer() {
    // window.setTimeout returns an Id that can be used to start and stop a timer
    warningTimerID = window.setTimeout(warningInactive, warningTimeout);
}

function warningInactive() {
    window.clearTimeout(warningTimerID);
    timeoutTimerID = window.setTimeout(IdleTimeout, timoutNow);
    $('#modalAutoLogout').modal('show');
}

function resetTimer() {
    window.clearTimeout(timeoutTimerID);
    window.clearTimeout(warningTimerID);
    startTimer();
}

// Logout the user.
function IdleTimeout() {
    document.getElementById('logout-form').submit();
}

function setupTimers () {
    document.addEventListener("mousemove", resetTimer, false);
    document.addEventListener("mousedown", resetTimer, false);
    document.addEventListener("keypress", resetTimer, false);
    document.addEventListener("touchmove", resetTimer, false);
    document.addEventListener("onscroll", resetTimer, false);
    startTimer();
}

$(document).on('click','#btnStayLoggedIn',function(){
    resetTimer();
    $('#modalAutoLogout').modal('hide');
});

$(document).ready(function(){
    setupTimers();
});

答案 4 :(得分:0)

只需使用此代码。

        var timeoutTimer;
        var expireTime = 1000*60*30;
        function expireSession(){
            clearTimeout(timeoutTimer);
            timeoutTimer = setTimeout("IdleTimeout()", expireTime);
        }
        function IdleTimeout() {
            localStorage.setItem("logoutMessage", true);
            window.location.href="{{url('logout')}}";
        }
        $(document).on('click mousemove scroll', function() {
            expireSession();
        });
        expireSession();