Hi,
Its better to set time for session clear, even though you want to set it while closing you could refer the below code
function CloseSession( )
{
document.getElementById('btnLogout').click();
}
window.onbeforeunload = CloseSession;
Hope this helps you a bit.
Regards,
RK