1

I'm using a timer function to check if the session is valid or not every five seconds.

setInterval(checksession,5000);

function checksession(called_for) {
    //alert('check-session')

        $.ajax({
            type:'POST'
            ,url:'CheckSession'
            ,success: validateresult
            ,data: { antiCSRF : '{{acsrf}}',
                   session_id: '{{session_id}}'}
            //,error: function(){ alert('Session check failed') }
            })
}

I would like to know what will happen if I have multiple ajax calls at the same time when the session is checked. Will it be 2 separate threads?

Is this the correct way to check session?

3
  • 5000 != "10 seconds"... It's also really unclear what you mean by "check session". Nothing about the usual definition of "sessions" involves sending a keep-alive every 10 seconds. If your session does time out after 10 seconds (none of them do, ever, because that would be useless), then sending a keep-alive in JavaScript defeats the purpose of having your session time out. I've voted to close as "primary opinion based", because nobody is going to be able to tell you if this is the "correct way to check session", because you haven't defined what that means. Commented Aug 12, 2014 at 18:57
  • That depends on how the server handles the request. What have you used to implement the page on the server, and what web server are you using? The only way that "checking the session" would make sense would be using a sessionless page, have you implemented "CheckSession" that way? Commented Aug 12, 2014 at 19:00
  • Its typo, its 5 seconds...In my application the session variables are kept in the server..The dict that maintains the sesion variables are updated each time when a request is called..If there is no request for 20 minutes I need redirect the user to the login page...TO attain this, I do a ajax call and check for the session.. Commented Aug 12, 2014 at 19:04

1 Answer 1

1

So first off, you're better off (imo) using setTimeout for this rather than setInterval. You really want your next check to happen x seconds after you have the answer from the previous check, not every x seconds regardless (b/c of server lag, latency on the network, whatever). Bottom line, imo it's better to do setTimeout then do another `setTimeout in the ajax callback.

JS is single threaded, so you won't do it in two separate threads, but you can have multiple ajax calls pending at once, which I think is what you mean.

Finally, on "correct". It really depends a bit on what you're trying to accomplish. In general, sessions with sliding expirations (the only real time that any 'check' to the server should matter, since otherwise they can get the expiry once and count the time on the client) are there to timeout after a period of inactivity. If you're having your script 'fake' activity by pinging the server every five seconds, then you might as well just set your expiry to infinity and not have it expire ever. Or set it to expire when the browser window closes, either way.

If you're trying to gracefully handle an expired session, the better way to handle it in Ajax is to just handle the 401 error the server should be sending you if you're not logged in anymore.

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

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.