李东's Blog

李东

JS来判断未操作直接退出账户

20
2025-04-01
JS来判断未操作直接退出账户
<script type="text/javascript">
    var lastTime = new Date().getTime();
        var currentTime = new Date().getTime();
        var timeOut = 10 * 60 * 1000; //设置超时时间: 10分

        $(function(){
            /* 鼠标移动事件 */
            $(document).mouseover(function(){
                lastTime = new Date().getTime(); //更新操作时间

            });
        });

        function testTime(){
            currentTime = new Date().getTime(); //更新当前时间
            if(currentTime - lastTime > timeOut){ //判断是否超时
                 $.ajax({
                    url:"{{}}",//走退出接口
                    dataType:"json",
                    type:"get",
                    async : false,
                    cache : false,
                    success:function(){
                        // debugger;
                            window.location.href="{{'admin/login'}}";
                    },
                    error:function(){

                    }
                })
            }
        }

        /* 定时器  间隔1秒检测是否长时间未操作页面  */
        window.setInterval(testTime, 1000);

</script>