网站10分钟不操作会话退出

方式一

    <script type="text/javascript">
        var maxTime = 10; // seconds
        var time = maxTime;
        $('body').on('keydown mousemove mousedown', function (e) {
            time = maxTime; // reset
        });
        var intervalId = setInterval(function () {
            time--;
            if (time <= 0) {
                ShowInvalidLoginMessage();
                clearInterval(intervalId);
            }
        }, 1000)
        function ShowInvalidLoginMessage() {
            // 清除cookie
            // 提示用户
            // 该干嘛干嘛
            alert('那么长时间没动弹,退出喽!');
            $.ajax({
                url: "RegisterCheck.csx?tag=logout",
                dataType: "text",
                success: function (response, p) {
                    window.location.href = "Login.html";
                }
            })
        }
    </script>

 方式二

function ReLogin() {
    window.location.href = "Login2.html";
};

var maxTime = 900; // seconds
var time = maxTime;
$(function () {
    /* 鼠标移动事件 */
    $(document).mouseover(function () {
        time = maxTime;
    });
    $(document).mousedown(function () {
        time = maxTime;
    });
});

var intervalId = setInterval(function () {
    time--;
    console.log("倒计时:" + time);
    if (time <= 0) {
        ShowInvalidLoginMessage();
        clearInterval(intervalId);
    }
}, 1000)
function ShowInvalidLoginMessage() {
    var title = '提示', content = '那么长时间没动弹,退出喽!';
    Ext.MessageBox.alert(title, content, function (r) {
        Ext.Ajax.request({
            url: "RegisterCheck.csx",
            params: {
                tag: "logout"
            },
            success: function () {
                ReLogin();
            }
        });
    });
}
原文地址:https://www.cnblogs.com/chenyanbin/p/12409857.html