JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
JavaScript
var _timer;
function addIdleCheckInMethod() {
var sessionTimeout = new checkInSessionTimout();
$(this).mousemove(sessionTimeout.resetTimer);
$(this).keypress(sessionTimeout.resetTimer);
/* _timer = setInterval(function () { sessionTimeout.checkIdleTime(onSessionExpire) }
, 1000);
*/
_timer = setInterval(sessionTimeout.checkIdleTime
, 1000, onSessionExpire);
}
function onSessionExpire() {
clearInterval(_timer);
var user = new user();
console.log('session expired!');
}
class checkInSessionTimout {
constructor(args){
this.IDLE_TIMEOUT = args != null && args.idleTimout ? args.idleTimout : 5;
this._idleSecondsCounter = 0;
console.log('constructor '+ this.IDLE_TIMEOUT );
}
checkIdleTime(callback) {
this._idleSecondsCounter++;
console.log( "idle timeout: " + this.IDLE_TIMEOUT + ' counter: ' + this._idleSecondsCounter);
if (this._idleSecondsCounter >= this.IDLE_TIMEOUT) {
callback();
}
}
resetTimer() {
console.log('reset:' + this._idleSecondsCounter);
this._idleSecondsCounter = 0;
}
}
addIdleCheckInMethod();