0

in an asp.net application I have a Jquery function.

I need to check if the session has timeout something like this

function Myfunction() {
    if(FuctionHasTimedOut)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}

How to do that? thanks a lot for any help

Y2theZ
  • 10,162
  • 38
  • 131
  • 200

3 Answers3

0

If mean server side Session state, then you can do ajax request to server in such a function:

function Myfunction() {
    $.post('{urlToHandler}', function(){
        // server should return some session value, that would indicate that session state  is accesible
    });
}
Sly
  • 15,046
  • 12
  • 60
  • 89
0

Try this plugin http://philpalmieri.com/js_sandbox/timedLogout/jquery-idleTimeout.js

/** Run with defaults **/
  $(document).ready(function(){
    $(document).idleTimeout();
  });


/** With Optional Overrides **/
  $(document).ready(function(){
    $(document).idleTimeout({
      inactivity: 30000,
      noconfirm: 10000,
      sessionAlive: 10000
    });
  });
coder
  • 13,002
  • 31
  • 112
  • 214
0

I would use a PageMethod for this. Here's some code I found here:

[WebMethod]
public static bool HasSessionTimedOut()
{
    HttpSessionState session = HttpContext.Current.Session;

    // I put this value into Session at the beginning.
    DateTime? sessionStart = session[SessionKeys.SessionStart] as DateTime?;

    bool isTimeout = false;

    if (!sessionStart.HasValue)
    {
        isTimeout = true;
    }
    else
    {
        TimeSpan elapsed = DateTime.Now - sessionStart.Value;
        isTimeout = elapsed.TotalMinutes > session.Timeout;
    }

    return isTimeout;
}

And this is my Javascript:

<script type="text/javascript">                                   
    $(function() {                                                 
        var callback = function(isTimeout) {
            if (isTimeout) {                           
                // Show your pop-up here...
            }                         
        };                                                         
        setInterval(                                                
            function() {
                // Don't forget to set EnablePageMethods="true" on your ScriptManager.
                PageMethods.HasSessionTimedOut(false, callback);    
            },                                                     
            30000                                                   
        );                                                          
    });                                                            
</script> 
Community
  • 1
  • 1
James Johnson
  • 45,496
  • 8
  • 73
  • 110