I would handle this on the client. Create an object to encapsulate the functionality.
Something like:
var UserInactivityMonitor = UserInactivityMonitor || {};
UserInactivityMonitor = (function (module) {
var inactivityIndex;
var promptIndex;
var timer;
module.startInactivityMonitor = function () {
module.timerTick();
};
module.timerTick = function () {
inactivityIndex--;
if (inactivityIndex === 0) {
module.fireInactivityAlert();
}
if (inactivityIndex === promptIndex) {
module.fireIdlePrompt();
}
timer = setTimeout(module.timerTick, 1000);
};
module.fireIdlePrompt = function () {
var response = confirm('are you stil there?');
if (response === true) {
module.resetInactivityIndex();
}
};
module.resetInactivityIndex = function () {
inactivityIndex = 15;
promptIndex = 5;
};
module.fireInactivityAlert = function () {
alert('Inactivity alert!');
};
module.initialize = function () {
module.resetInactivityIndex();
module.startInactivityMonitor();
};
return module;
})(UserInactivityMonitor || {});
Set inactivityIndex to the number of seconds that will pass before the inactivity event fires. Set promptIndex to the number of seconds remaining when the user will be prompted if they are still there. The code above sets the inactivity timeout to 15 seconds and a idle prompt will be invoked at the 5 second remaining mark.
On page load start the inactivity timer:
$(function () {
UserInactivityMonitor.initialize();
});
On any AJAX request, reset the counter:
$("#fooButton").on('click', function () {
$.ajax(
{
url: $("#buttonClickPostUrl").val(),
data: {
someData: 'data'
},
type: 'POST',
complete: function () {
UserInactivityMonitor.resetInactivityIndex();
}
});
});
If the server is maintaining session state then you will want to make a request back to the server to kill the session and optionally direct the browser to the appropriate page for the event. You would do this in the fireInactivityAlert() function.