PDA

View Full Version : using delayedTask for client side timeout(like session handling)



mohaskuar
18 Mar 2013, 2:48 AM
var logout_check = 5, // in seconds
window_timer = 2;
var show_window = new Ext.util.DelayedTask(function(){
var wintimer = setTimeout(function(){window.location='index.html'},window_timer*1000);
Ext.Msg.show({
title:'Inactivity',
msg: 'Click OK to carry on working or Cancel to logout',
buttons: Ext.Msg.OKCANCEL,
icon: Ext.MessageBox.WARNING,
fn: function(btn,text){
if(btn=='ok'){
clearTimeout(wintimer);
show_window.delay(logout_check*1000);
}else{
window.location='index.html';
}


}
});
});


Ext.getDoc().on('click', function(){
show_window.delay(logout_check*1000);
});


show_window.delay(logout_check*1000);


i have put the above code on launch function of app.js in my mvc based application....evry thing works good but aftertimeout a user is redirected to a login page the DelayedTask function still excutes and message boxes keep showing up..
what can i do to make it conditional..i want this only to run when a user is already loged in..and stop when a timeout which redirect to index.html--->login page

grgur
18 Mar 2013, 4:11 AM
To cancel the DelayedTask, use show_window.cancel();

Taking a few steps back, this is a pretty straight forward process. You poll the server in given time intervals to announce existence. The server replies with a simple JSON object that is either:
a) Everything's fine (success: true) (you can also feed additional actions here such as chat messages or syncing data)
b) Something is not alright, I'm removing the session (success: false)

Your client end could track for mouse movements and clicks in order to guess that the user is still active. Then give another chance with a popup dialog to gain attention. Useful if the user is reading a lengthier text or similar. If everything fails and the user is obviously not responsive, than you can log them out forcefully, initiated from the client side, and confirmed on the server.

You're likely to need a flag or two to make it happen, but it all depends on your architecture and even coding habits.

I hope I can encourage you when I say that I'm sure it's not a big deal for you, you can do it!
;)