5

プラグインを使用せずに、プレーンなjQueryでsessionTimeOut機能を作成するにはどうすればよいですか? 独自のアラート ui.To を使用して、記述できる 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

ローストの回答に基づいていますが、mousemove の代わりに 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 に答える