How to detect idle time in JavaScript elegantly?

前端 未结 30 2334
别跟我提以往
别跟我提以往 2020-11-21 11:51

Is it possible to detect \"idle\" time in JavaScript?
My primary use case probably would be to pre-fetch or preload content.

Idle time:

相关标签:
30条回答
  • 2020-11-21 12:53

    (Partially inspired by the good core logic of Equiman earlier in this thread.)

    sessionExpiration.js


    sessionExpiration.js is lightweight yet effective and customizable. Once implemented, use in just one row:

    sessionExpiration(idleMinutes, warningMinutes, logoutUrl);
    
    • Affects all tabs of the browser, not just one.
    • Written in pure JavaScript, with no dependencies. Fully client side.
    • (If so wanted.) Has warning banner and countdown clock, that is cancelled by user interaction.
    • Simply include the sessionExpiration.js, and call the function, with arguments [1] number of idle minutes (across all tabs) until user is logged out, [2] number of idle minutes until warning and countdown is displayed, and [3] logout url.
    • Put the CSS in your stylesheet. Customize it if you like. (Or skip and delete banner if you don't want it.)
    • If you do want the warning banner however, then you must put an empty div with ID sessExpirDiv on your page (a suggestion is putting it in the footer).
    • Now the user will be logged out automatically if all tabs have been inactive for the given duration.
    • Optional: You may provide a fourth argument (URL serverRefresh) to the function, so that a server side session timer is also refreshed when you interact with the page.

    This is an example of what it looks like in action, if you don't change the CSS.

    0 讨论(0)
  • 2020-11-21 12:54

    I have created a small lib that does this a year ago:

    https://github.com/shawnmclean/Idle.js

    Description:

    Tiny javascript library to report activity of user in the browser (away, idle, not looking at webpage, in a different tab, etc). that is independent of any other javascript libraries such as jquery.

    Visual Studio users can get it from NuGet by: PM> Install-Package Idle.js

    0 讨论(0)
  • 2020-11-21 12:54

    Here is a rough jQuery implementation of tvanfosson's idea:

    $(document).ready(function(){
    
       idleTime = 0;
    
       //Increment the idle time counter every second.
       var idleInterval = setInterval(timerIncrement, 1000);
    
       function timerIncrement()
       {
         idleTime++;
         if (idleTime > 2)
         {
           doPreload();
         }
       }
    
       //Zero the idle timer on mouse movement.
       $(this).mousemove(function(e){
          idleTime = 0;
       });
    
       function doPreload()
       {
         //Preload images, etc.
       }
    
    })
    
    0 讨论(0)
  • 2020-11-21 12:55

    Here is a simple script using JQuery that handles mousemove and keypress events. If the time expires, the page reload.

    <script type="text/javascript">
    var idleTime = 0;
    $(document).ready(function () {
        //Increment the idle time counter every minute.
        var idleInterval = setInterval(timerIncrement, 60000); // 1 minute
    
        //Zero the idle timer on mouse movement.
        $(this).mousemove(function (e) {
            idleTime = 0;
        });
        $(this).keypress(function (e) {
            idleTime = 0;
        });
    });
    
    function timerIncrement() {
        idleTime = idleTime + 1;
        if (idleTime > 19) { // 20 minutes
            window.location.reload();
        }
    }
    </script>   
    
    0 讨论(0)
  • 2020-11-21 12:55

    You can do it more elegantly with underscore and jquery-

    $('body').on("click mousemove keyup", _.debounce(function(){
        // do preload here
    }, 1200000)) // 20 minutes debounce
    
    0 讨论(0)
  • 2020-11-21 12:56

    You can use the below mentioned solution

    var idleTime;
    $(document).ready(function () {
             reloadPage();
            $('html').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
                clearTimeout(idleTime);
                reloadPage();
            });
    });
    function reloadPage() {
        clearTimeout(idleTime);
        idleTime = setTimeout(function () {
            location.reload();
        }, 3000);
    }
    
    0 讨论(0)
提交回复
热议问题