1

How can I do this in Asp.Net MVC?

  • Activity Timer : When user navigates on the page (means doing something that calls ActionResult) the timer will reset.
  • When the user doesn't do any activity in more than 15 minutes, he will be prompted and be asked "Are you still there?" (Yes/LogOut), if he click yes, then activity timer will go back to zero, and starting counting again.
  • This activity timer must be unique per user and must be destroy upon logging out.

PLEASE PLEASE! Be PATIENT TO ME! I don't know how to this.

I had posted this question but I didn't get any answer that solves this problem. So I plan to do it from the start, and I need some help/guide to do it properly.
Any reference links or tutorials will be much appreciated!

1
  • I may be missing something, but I think you can do this without action filter. I would guess the timer would be reset upon each view rendering (if logged in), fire after fifteen mins, then reset after they answer. If so, I can think of a fairly simply design. Commented Feb 9, 2013 at 5:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you mean I have to do this $(document).click(function() { //Call UserInactivityMonitor }); catch all click events on the page and consider it as an activity, then reset the timer?
Not exactly. Are you referring to the ajax request code sample or the page load sample? Whenever a full page load occurs (IE, first time the use requests the page) you want to start the inactivity timer. The page load sample shows how to do this using jQuery. The ajax code sample shows how to reset the inactivity counter on completion of an ajax request (failure or not), again the sample is showing an ajax request utilizing jquery. You would have to mold that sample into your existing code base. The take-away from the ajax sample is the resetInactivityIndex() function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.