普通jQuery中的会话超时

时间:2013-07-19 12:34:37

标签: jquery

如何在不使用任何插件的情况下在普通jQuery中编写sessionTimeOut功能?我想使用自己的警报ui。要弄清楚我可以写的任何UI活动,但不知道如何在超时功能中将它组合起来。

$('*').on('click', function () {
    console.log('click', this);

});

$('*').on('change', function() {
    console.log('change', this);
});

2 个答案:

答案 0 :(得分:6)

您可以在所有现代浏览器上捕获mousemove事件:

(function () {
    var timeoutSession;
    document.addEventListener('mousemove', function (e) {
        clearTimeout(timeoutSession);
        timeoutSession = setTimeout(function () {
            alert('Make SESSION expire');
            //call a script here to server...
        }, 30000); //30s
    }, true);
})();

答案 1 :(得分:0)

基于焙烧的答案,但使用ajax调用作为计时器重置动作而不是mousemoves:

(function (delay) {

    function getTimeout() {
        return setTimeout(function () {
            console.log('Make SESSION expire');
            //call a script here to server...
        }, delay);
    };

    // start the session timer
    var timeoutSession = getTimeout();

    $(document).ajaxSend(function() {
        // this event fires any time an ajax call is sent
        console.log('timeout reset');
        clearTimeout(timeoutSession);
        timeoutSession = getTimeout();
    });
})(30000); // <--- notice that you put the timeout delay here
相关问题