javascript - jquery 空闲秒数计数器

标签 javascript jquery idle-timer

我在空闲库中有这个函数,但我需要的是计算以秒为单位的 Action 时间,我的意思是事件时间(onclick、onscroll 和 keypress)。

功能是:

    (function () { 
var minutes = false;
var interval = 1000; 
var IDLE_TIMEOUT = 5; 
var idleCounter = 0;
var counter=0;

document.onclick = document.onkeypress = function () {
    idleCounter = 0;
    setInterval(function () {
++counter;; 
 }, 1000);

};

window.setInterval(function () {
    if (++idleCounter >= IDLE_TIMEOUT) {
    alert(counter);
        document.location.href = "SessionExpired.aspx";
    }
}, interval);
}());

这个函数将等待5秒,如果页面上没有任何操作,那么我将被重定向到SessionExpired.aspx。如果有 Action ,那么每秒都会执行++couter。

我需要这个以秒为单位的计数器。

谢谢。

最佳答案

您只需重置计时器即可

var counter;
var counterSeconds;

document.onclick = document.onkeypress = function () {
    idleCounter = 0; // Set counter to 0 on each keypress/page click
    clearInterval(counter) // Every time they click, clear the old interval and start a new one below
    counter = setInterval(function () { // assign the interval to a variable so we can clear it
       if (idleCounter > IDLE_TIMEOUT) { // Check if they've idled too long
            document.location.href = "SessionExpired.aspx"; // Redirect them if they have
       } 
       ++counterSeconds;
       ++idleCounter; // Should increment 1/sec depending on your computer.
    }, 1000); // Ticks at 1000 milliseconds (1 Second)
};

关于javascript - jquery 空闲秒数计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680700/

相关文章:

javascript - 带 react 导航的 DrawerNavigation 标题

javascript - openlayers 3 中带有标签或文本的图标,偏移量

javascript - 如何设置一个非常快的 node.js UDP 服务器

jquery - 使用 jQuery DataTables 恢复原始排序顺序

c - 检索用户空闲时间的多平台方式

javascript - jQuery:循环具有不同超时值的循环

javascript - jScrollPane 和窗口调整大小触发器

javascript - jQuery 插件返回 this.each

iphone - 帮助解决我的应用崩溃的原因?

iphone - 我可以在我的应用中禁止系统警报吗?