5

如何在不使用任何插件的情况下用纯 jQuery 编写 sessionTimeOut 功能?我想使用我自己的警报 ui。找出我可以编写的任何 UI 活动,但不确定如何将它组合到超时功能中。

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

});

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

2 回答 2

7

您可以在所有现代浏览器上捕获 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);
})();
于 2013-07-19T12:42:14.013 回答
1

基于烤的答案,但使用 ajax 调用作为您的计时器重置操作而不是鼠标移动:

(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
于 2013-07-19T12:42:20.290 回答