JavaScript automatically logs out after a long time of inactivity

Web page automatic timeout and automatic exit method

Idea: 
Use the mouseover event to monitor whether there is a user operating the page, write a timer at a specific time interval to detect whether the page has not been operated for a long time, and if so, exit; the 
specific time code is as follows (js):

  

     var oldTime = new Date().getTime();
         var newTime = new Date().getTime();
         var outTime = 8 * 60 * 1000; // set timeout: 8 minutes 

        $( function (){
             /* mouse Move event */ 
            $(document).mouseover( function (){
                oldTime = new Date().getTime(); //Mouse-in resets the dwell time

            });
        });

        function OutTime(){
            newTime = new Date().getTime(); // Update the current time without operation 
            if (newTime - oldTime > outTime){ // Determine whether the timeout does not operate 
                console.log("Time out, log out" );
            }
        }

        /* The timer determines whether the page operation has not been performed for a long time every 5 seconds */ 
        window.setInterval(OutTime, 5000);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816807&siteId=291194637