是否可以在 JavaScript 中检测“空闲”时间?
我的主要用例可能是预取或预加载内容。
我将空闲时间定义为用户不活动或没有任何 CPU 使用率的时间段
是否可以在 JavaScript 中检测“空闲”时间?
我的主要用例可能是预取或预加载内容。
我将空闲时间定义为用户不活动或没有任何 CPU 使用率的时间段
这是一个使用 jQuery 处理 mousemove 和 keypress 事件的简单脚本。如果时间到期,页面会重新加载。
<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>
不使用 jQuery,只使用原生 JavaScript:
var inactivityTime = function () {
    var time;
    window.onload = resetTimer;
    // DOM Events
    document.onmousemove = resetTimer;
    document.onkeydown = resetTimer;
    function logout() {
        alert("You are now logged out.")
        //location.href = 'logout.html'
    }
    function resetTimer() {
        clearTimeout(time);
        time = setTimeout(logout, 3000)
        // 1000 milliseconds = 1 second
    }
};
并在需要的地方初始化函数(例如:onPageLoad)。
window.onload = function() {
  inactivityTime();
}
如果需要,您可以添加更多 DOM 事件。最常用的是:
document.onload = resetTimer;
document.onmousemove = resetTimer;
document.onmousedown = resetTimer; // touchscreen presses
document.ontouchstart = resetTimer;
document.onclick = resetTimer;     // touchpad clicks
document.onkeydown = resetTimer;   // onkeypress is deprectaed
document.addEventListener('scroll', resetTimer, true); // improved; see comments
或使用数组注册所需的事件
window.addEventListener('load', resetTimer, true);
var events = ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart'];
events.forEach(function(name) {
 document.addEventListener(name, resetTimer, true);
});
DOM 事件列表:http : //www.w3schools.com/jsref/dom_obj_event.asp
请记住使用window,或document根据您的需要。在这里你可以看到它们之间的区别:JavaScript 中的 window、screen 和 document之间有什么区别?
代码更新@frank-conijn 和@daxchen 改进:window.onscroll如果滚动在可滚动元素内,则不会触发,因为滚动事件不会冒泡。在 中window.addEventListener('scroll', resetTimer, true),第三个参数告诉侦听器在捕获阶段而不是冒泡阶段捕获事件。
function idleLogout() {
    var t;
    window.onload = resetTimer;
    window.onmousemove = resetTimer;
    window.onmousedown = resetTimer;  // catches touchscreen presses as well      
    window.ontouchstart = resetTimer; // catches touchscreen swipes as well 
    window.onclick = resetTimer;      // catches touchpad clicks as well
    window.onkeydown = resetTimer;   
    window.addEventListener('scroll', resetTimer, true); // improved; see comments
    function yourFunction() {
        // your function for too long inactivity goes here
        // e.g. window.location.href = 'logout.php';
    }
    function resetTimer() {
        clearTimeout(t);
        t = setTimeout(yourFunction, 10000);  // time is in milliseconds
    }
}
idleLogout();
除了有关活动检测的改进以及从document到的更改之外window,该脚本实际上调用了该函数,而不是让它闲置。
它不会直接捕获零 CPU 使用率,但这是不可能的,因为执行函数会导致 CPU 使用率。用户不活动最终会导致 CPU 使用率为零,因此它确实间接地捕获了零 CPU 使用率。
我创建了一个小型库来执行此操作:
https://github.com/shawnmclean/Idle.js
描述:
用于报告用户在浏览器中的活动的微型 JavaScript 库(离开、空闲、不看网页、在不同的选项卡中等)。它独立于任何其他 JavaScript 库,例如 jQuery。
Visual Studio 用户可以通过以下方式从 NuGet 获取它:
Install-Package Idle.js
这是tvanfosson 想法的一个粗略的 jQuery 实现:
$(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.
   }
   
})