最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - automatically logout from page - Stack Overflow

programmeradmin1浏览0评论

I want to automatically after being sometimes idle by the user but not being able to do.I has used follwing javascript but nothing is going on and I have also add session timeout in web config but its also not working.Please give me some ideas.

<script type="text/javascript">
        var timer1, timer2;
        document.onkeypress=resetTimer;
        document.onmousemove=resetTimer;
        function resetTimer()
        {
            document.getElementById('timeoutPopup').style.display='none';
            clearTimeout(timer1);
            clearTimeout(timer2);

            // waiting time in minutes
            var wait=10;

            // alert user one minute before
            timer1=setTimeout("alertUser()", (60000*wait)-1);

            // logout user
            timer2=setTimeout("logout()", 60000*wait);
        }

        function alertUser()
        {
            document.getElementById('timeoutPopup').style.display='block';
        }

        function logout()
        {
            window.location.href='Logout.aspx';
        }



}



        </script>

I want to automatically after being sometimes idle by the user but not being able to do.I has used follwing javascript but nothing is going on and I have also add session timeout in web config but its also not working.Please give me some ideas.

<script type="text/javascript">
        var timer1, timer2;
        document.onkeypress=resetTimer;
        document.onmousemove=resetTimer;
        function resetTimer()
        {
            document.getElementById('timeoutPopup').style.display='none';
            clearTimeout(timer1);
            clearTimeout(timer2);

            // waiting time in minutes
            var wait=10;

            // alert user one minute before
            timer1=setTimeout("alertUser()", (60000*wait)-1);

            // logout user
            timer2=setTimeout("logout()", 60000*wait);
        }

        function alertUser()
        {
            document.getElementById('timeoutPopup').style.display='block';
        }

        function logout()
        {
            window.location.href='Logout.aspx';
        }



}



        </script>
Share Improve this question asked Mar 18, 2014 at 6:30 user3247426user3247426 1273 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

First argument for setTimeout is a function handle. JS Timing

<script type="text/javascript">
        var timer1, timer2;
        document.onkeypress=resetTimer;
        document.onmousemove=resetTimer;
        function resetTimer()
        {
           document.getElementById('timeoutPopup').style.display='none';
           clearTimeout(timer1);
           clearTimeout(timer2);
                // waiting time in minutes
            var wait=10;

           // alert user one minute before
            timer1=setTimeout(alertUser, (60000*wait)-1);

            // logout user
            timer2=setTimeout(logout, 60000*wait);
        }

        function alertUser()
        {
            document.getElementById('timeoutPopup').style.display='block';
        }

        function logout()
        {
            window.location.href='Logout.aspx';
        }



} </script>

You can achieve this by adding the following code in web config:

 <system.web>
    <sessionState timeout="10"></sessionState>
 </system.web>

Here timeout=10 implies after 10 Minutes, the session will be expired and therefore, automatic logout will be acplished

I think you can use html meta tag for this

<meta http-equiv="refresh" content="30">

This will cause your page to refresh after 30 seconds (change time as you need) and while refreshing you can check the session in your server side and can do the logic what you want.

发布评论

评论列表(0)

  1. 暂无评论