Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何知道浏览器空闲时间?_Javascript_Browser - Fatal编程技术网

Javascript 如何知道浏览器空闲时间?

Javascript 如何知道浏览器空闲时间?,javascript,browser,Javascript,Browser,如何跟踪浏览器空闲时间?我正在使用IE8 我没有使用任何会话管理,也不想在服务器端处理它。希望这就是您想要的 下面是跟踪空闲时间的纯JavaScript方法,当空闲时间达到一定限制时,执行一些操作: var IDLE\u TIMEOUT=60//秒 var _idleSecondsTimer=null; var_idleSecondsCenter=0; document.onclick=函数(){ _IDLESecondsCenter=0; }; document.onmousemove=函数

如何跟踪浏览器空闲时间?我正在使用IE8


我没有使用任何会话管理,也不想在服务器端处理它。

希望这就是您想要的
下面是跟踪空闲时间的纯JavaScript方法,当空闲时间达到一定限制时,执行一些操作:

var IDLE\u TIMEOUT=60//秒
var _idleSecondsTimer=null;
var_idleSecondsCenter=0;
document.onclick=函数(){
_IDLESecondsCenter=0;
};
document.onmousemove=函数(){
_IDLESecondsCenter=0;
};
document.onkeypress=函数(){
_IDLESecondsCenter=0;
};
_idleSecondsTimer=window.setInterval(检查IdleTime,1000);
函数CheckIdleTime(){
_idleSecondsCenter++;
var oPanel=document.getElementById(“SecondsUntilExpire”);
if(oPanel)
oPanel.innerHTML=(空闲\u超时-\u空闲秒中心)+”;
如果(\u idleSecondsCenter>=空闲\u超时){
窗口清除间隔(_idleSecondsTimer);
警报(“时间已过!”);
document.location.href=“logout.html”;
}
}
#secondsuntille{背景色:黄色;}

您将在几秒钟内自动注销。
回复太晚,但这可能有助于编写干净实用的解决方案。当您不需要显示会话到期的剩余时间时,这是一个理想的解决方案。可以忽略
setInterval()
,它会在应用程序运行期间一直运行脚本

var inactivityTimeOut=10*1000,//10秒
inactivitySessionExpireTimeOut='';
setSessionExpireTimeOut();
函数setSessionExpireTimeOut(){
"严格使用",;
clearSessionExpireTimeout();
inactivitySessionExpireTimeOut=setTimeout(函数(){
expireSessionIdleTime();
},不活动时间);
}
函数expireSessionIdleTime(){
"严格使用",;
log('user inactive for'+inactivityTimeOut+“秒”);
log(“会话已过期”);
警报(“时间到期”);
clearSessionExpireTimeout();
document.location.href=“logout.html”;
}
函数clearSessionExpireTimeout(){
"严格使用",;
clearTimeout(inactivitySessionExpireTimeOut);
}

运行示例:超时警报将在10秒后弹出
以下是一种使用jquery的方法,我需要保留文档上现有的键盘事件

我还需要在不同的空闲时间做不同的事情,所以我把它包装在一个函数中

var onIdle = function (timeOutSeconds,func){
    //Idle detection
    var idleTimeout;
    var activity=function() {
        clearTimeout(idleTimeout);
        console.log('to cleared');
        idleTimeout = setTimeout(func, timeOutSeconds * 1000);
    }
    $(document).on('mousedown mousemove keypress',activity);
    activity();
}
onIdle(60*60,function(){
    location.reload();
});
onIdle(30,function(){
    if(currentView!=='welcome'){
        loadView('welcome');
    }
});

我需要一个类似的东西并创建了这个:

它简单、可配置、功能强大,没有任何依赖性。这里有一个例子

import { Idle } from 'idlejs/dist';

// with predefined events on `document`
const idle = new Idle()
  .whenNotInteractive()
  .within(60)
  .do(() => console.log('IDLE'))
  .start();
您还可以使用自定义事件目标和事件

const idle = new Idle()
  .whenNot([{
    events: ['click', 'hover'],
    target: buttonEl,
  },
  {
    events: ['click', 'input'],
    target: inputEl,
  },
  ])
  .within(10)
  .do(() => called = true)
  .start();

(用typescript编写并编译为es5)

您希望如何实现会话管理而不在服务器端处理它?我的意思是,您可以将超时设置为60秒,而不是每秒调用
CheckIdleTime()
。我想你不能显示剩余的时间,但它要简单得多。您的解决方案也与OP似乎要问的有点不同。@CJRamki只是有
if(确认(“确定要离开?”){document.location.href=“logout.html”;}
@Yegya但是我将无法显示剩余的时间量,就像我在示例代码中所做的那样。虽然这不是很重要,但是如果没有
setInterval()
@ShadowWizard有人拥有你的代码,并且比你有更高的声誉。只是让你知道。@ChetanPatel只有在iframe包含同一域中的页面时才知道。实际上,当你可以用非常简单的脚本实现同样的效果时,忽略插件是很好的。@Yegya一般来说,这是正确的,但是这个插件是由Paul Irish编写的,他是well在业界很有名,所以我对他的代码更有信心。