0

Perhaps I am over-thinking this but what I need to do is see what the time difference it is from the start session time (1 minute) subtracting the current time.

<script type="text/javascript">
var mySessionTimer;

@functions {        
    public int PopupShowDelay
    {
        get {
            DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
            DateTime currentServerTime = DateTime.Now;
            TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));

            return 60000 * (int)duration.TotalMinutes;
        }
    }
}

function callJSSessionTimer() {
    var sessionTimeoutWarning = @PopupShowDelay;
    var sTimeout = parseInt(sessionTimeoutWarning);

    mySessionTimer = setTimeout('SessionEnd()', sTimeout);
}

function SessionEnd() {
    clearTimeout(mySessionTimer);
    window.location = "/Account/sessionover";
}

@if (userInfo != null)
{
    if (userInfo.chosenAMT == "True")
    {
        @:callJSSessionTimer();
    } else
    {
        @:clearTimeout(mySessionTimer);
    }
} else {
    @:clearTimeout(mySessionTimer);
}
</script>

So for the value for duration is -00:01:00 which technically is correct since currentSetTimeout is 1 minute and it gets todays date/time which is a minute away since its subtracting it fromcurrentSetTimeout.

So the point of all of this is to keep track of the remaining session time when the user jumps from page to page. Currently when the user goes to another page, it resets the time and its not accurate.

How can I go about doing this the way I need it?

3 Answers 3

1

You may use html5 session storage to maintain the value when a user goes to another page during the session:

if (sessionStorage.popupShowDelay) {        
    sessionStorage.popupShowDelay = Number(sessionStorage.clickcount);
} else {
    DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
    DateTime currentServerTime = DateTime.Now;
    TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
    sessionStorage.popupShowDelay = 60000 * (int)duration.TotalMinutes;
}

You can see more in formation here : https://www.w3schools.com/html/html5_webstorage.asp

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

Comments

0

Conceptually your problem is the counter would reset as you go from page to page. So you have to keep start time of the session server side. If you are using ASP.NET, you'd use a session variable to do this. Other platforms have similar things. They all work by using cookies. Good luck!

2 Comments

An example would be great instead of just telling. And my title says I am using ASP.net MVC...
You have a few things to learn and a simple example isn't going to help you. You have to understand the ASP.NET session variable, how to access it from an MVC controller, how to hook global events. It's quite a bit and it won't fit in the size of these comments.
0

Got it!

@functions {        
        public int PopupShowDelay
        {
            get {
                if (Session["currentSetTimeout"] != null)
                {
                    DateTime dt1 = DateTime.Parse(Session["currentSetTimeout"].ToString());
                    DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
                    TimeSpan span = dt1 - dt2;

                    return (int)span.TotalMilliseconds;
                }

                return 0;
            }
        }
    }

Comments

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.