Session timeout in plain jQuery

How to write sessionTimeOut function in plain jQuery without using any connections? I want to use my own ui alert. To find out which user interface I can write, but don’t know how to combine it into timeout functions.

$('*').on('click', function () { console.log('click', this); }); $('*').on('change', function() { console.log('change', this); }); 
+4
source share
2 answers

You can capture the mousemove event in all modern browsers:

 (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); })(); 
+6
source

Based on a fried answer, but using ajax calls as your timer, reset the action instead of 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 
0
source

Source: https://habr.com/ru/post/1492299/


All Articles