如何在纯jQuery中编写sessionTimeOut功能而不使用任何插件?我想使用自己的警告UI。为了确定任何UI活动,我可以编写代码,但不确定如何将其与超时功能结合起来。
$('*').on('click', function () {
console.log('click', this);
});
$('*').on('change', function() {
console.log('change', this);
});
如何在纯jQuery中编写sessionTimeOut功能而不使用任何插件?我想使用自己的警告UI。为了确定任何UI活动,我可以编写代码,但不确定如何将其与超时功能结合起来。
$('*').on('click', function () {
console.log('click', this);
});
$('*').on('change', function() {
console.log('change', this);
});
您可以在所有现代浏览器上捕获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);
})();
根据Roasted的回答,但是使用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