我在空闲库中有这个函数,但我需要的是计算以秒为单位的 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/