Javascript 如何在给定的非活动期后自动重新加载页面
如果在给定的时间段内网页上没有活动,我如何自动重新加载网页?Javascript 如何在给定的非活动期后自动重新加载页面,javascript,ajax,Javascript,Ajax,如果在给定的时间段内网页上没有活动,我如何自动重新加载网页? <script type="text/javascript"> var timeout = setTimeout("location.reload(true);",600000); function resetTimeout() { clearTimeout(timeout); timeout = setTimeout("location.reload(true);",600000); } &l
<script type="text/javascript">
var timeout = setTimeout("location.reload(true);",600000);
function resetTimeout() {
clearTimeout(timeout);
timeout = setTimeout("location.reload(true);",600000);
}
</script>
var timeout=setTimeout(“location.reload(true);”,600000);
函数resetTimeout(){
clearTimeout(超时);
timeout=setTimeout(“location.reload(true);”,600000);
}
除非调用resetTimeout(),否则上述操作将每10分钟刷新一次页面。例如:
<a href="javascript:;" onclick="resetTimeout();">clicky</a>
这可以在没有javascript的情况下通过以下metatag实现:
<meta http-equiv="refresh" content="5" >
其中content=“5”是页面在刷新之前等待的秒数
但是你说只有在没有活动的情况下,活动才是什么类型的?如果你想在没有活动的情况下刷新页面,那么你需要弄清楚如何定义活动。假设我们每分钟刷新一次页面,除非有人按键或移动鼠标。这将使用jQuery进行事件绑定:
<script>
var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function(e) {
time = new Date().getTime();
});
function refresh() {
if(new Date().getTime() - time >= 60000)
window.location.reload(true);
else
setTimeout(refresh, 10000);
}
setTimeout(refresh, 10000);
</script>
var time=new Date().getTime();
$(document.body).bind(“mousemove按键”,函数(e){
时间=新日期().getTime();
});
函数刷新(){
如果(新日期().getTime()-时间>=60000)
window.location.reload(true);
其他的
设置超时(刷新,10000);
}
设置超时(刷新,10000);
我还构建了一个不需要jquery的完整javascript解决方案。也许可以把它变成一个插件。我用它来进行流体自动刷新,但它看起来可以帮助你
//刷新率是您希望刷新页面的频率
//关闭用户的不活动状态。
var刷新率=200// 这个任务非常容易,在html标题部分使用下面的代码
<head> <meta http-equiv="refresh" content="30" /> </head>
它将在30秒后刷新您的页面
是的,亲爱的,那么你必须使用Ajax技术。更改
特定html标记:
Ajax页面
setInterval(函数(){autolappage();},30000);//它将在每30秒后调用函数autoload()。
函数autolappage(){
$.ajax({
url:“目标页面的url”,
类型:“POST”,
成功:功能(数据){
$(“div#wrapper”).html(数据);//这里的wrapper是主div
}
});
}
内容将自动更改。
每次移动鼠标时,它都会检查上次移动鼠标的时间。如果时间间隔大于20’,它将重新加载页面,否则它将在上次移动鼠标时更新。 < P>我会考虑<代码>活动< /代码>是用户是否聚焦在窗口上。例如,当您从一个窗口单击到另一个窗口时(例如,从Google Chrome到iTunes,或从internet浏览器中的选项卡1到选项卡2),网页会发送一个回调,说“我没有对焦!”或“我对焦!”。可以使用jQuery利用这种可能缺少的活动来做任何他们想做的事情。如果我处在你的位置,我会使用下面的代码每5秒检查一次焦点,如果没有焦点,则重新加载
var window_focus;
$(window).focus(function() {
window_focus = true;
}).blur(function() {
window_focus = false;
});
function checkReload(){
if(!window_focus){
location.reload(); // if not focused, reload
}
}
setInterval(checkReload, 5000); // check if not focused, every 5 seconds
基于arturnt的公认答案。这是一个稍微优化的版本,但基本上做了相同的事情:
var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function () {
time = new Date().getTime();
});
setInterval(function() {
if (new Date().getTime() - time >= 60000) {
window.location.reload(true);
}
}, 1000);
唯一的区别是,此版本使用了setInterval
而不是setTimeout
,这使得代码更加紧凑。使用您选择的目标自动重新加载。在这种情况下,目标是\u self
设置为自身,但是您可以通过简单地更改窗口.open('self.location','u self')来更改重新加载页面代码>代码类似于此示例window.top.location=“window.open('http://www.YourPageAdress.com“,”自我“,
带有确认警报消息:
<script language="JavaScript">
function set_interval() {
//the interval 'timer' is set as soon as the page loads
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
itimer=setInterval("auto_logout()",timeoutMins);
atimer=setInterval("alert_idle()",timeout1Mins);
}
function reset_interval() {
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove 2. mouseclick 3. key press 4. scrolling
//first step: clear the existing timer
clearInterval(itimer);
clearInterval(atimer);
//second step: implement the timer again
itimer=setInterval("auto_logout()",timeoutMins);
atimer=setInterval("alert_idle()",timeout1Mins);
}
function alert_idle() {
var answer = confirm("Session About To Timeout\n\n You will be automatically logged out.\n Confirm to remain logged in.")
if (answer){
reset_interval();
}
else{
auto_logout();
}
}
function auto_logout() {
//this function will redirect the user to the logout script
window.open('self.location', '_self');
}
</script>
<script language="JavaScript">
function set_interval() {
//the interval 'timer' is set as soon as the page loads
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
itimer=setInterval("auto_logout()",timeoutMins);
}
function reset_interval() {
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove 2. mouseclick 3. key press 4. scrolling
//first step: clear the existing timer
clearInterval(itimer);
clearInterval(atimer);
//second step: implement the timer again
itimer=setInterval("auto_logout()",timeoutMins);
}
function auto_logout() {
//this function will redirect the user to the logout script
window.open('self.location', '_self');
}
</script>
<body onLoad="set_interval(); document.form1.exp_dat.focus();" onKeyPress="reset_interval();" onmousemove="reset_interval();" onclick="reset_interval();" onscroll="reset_interval();">
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout("IdleWarning()", timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Show idle timeout warning dialog.
function IdleWarning() {
var answer = confirm("Session About To Timeout\n\n You will be automatically logged out.\n Confirm to remain logged in.")
if (answer){
ResetTimers();
}
else{
IdleTimeout();
}
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout(timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
函数集_interval(){
//页面加载后立即设置间隔“计时器”
var timeoutMins=1000*1*15;//15秒
var timeout1Mins=1000*1*13;//13秒
itimer=setInterval(“auto_logout()”,timeoutMins);
atimer=setInterval(“警报空闲()”,超时1分钟);
}
函数重置_间隔(){
var timeoutMins=1000*1*15;//15秒
var timeout1Mins=1000*1*13;//13秒
//重置计时器。在以下每个事件中重置计时器:
//1.鼠标移动2.鼠标单击3.按键4.滚动
//第一步:清除现有计时器
清除间隔(itimer);
间隙(atimer);
//第二步:再次执行计时器
itimer=setInterval(“auto_logout()”,timeoutMins);
atimer=setInterval(“警报空闲()”,超时1分钟);
}
函数警报_idle(){
var answer=confirm(“会话即将超时\n\n您将自动注销。\n确认以保持登录。”)
若有(答复){
重置间隔();
}
否则{
自动退出();
}
}
函数自动_注销(){
//此函数将用户重定向到注销脚本
window.open('self.location','u self');
}
无确认警报:
<script language="JavaScript">
function set_interval() {
//the interval 'timer' is set as soon as the page loads
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
itimer=setInterval("auto_logout()",timeoutMins);
atimer=setInterval("alert_idle()",timeout1Mins);
}
function reset_interval() {
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove 2. mouseclick 3. key press 4. scrolling
//first step: clear the existing timer
clearInterval(itimer);
clearInterval(atimer);
//second step: implement the timer again
itimer=setInterval("auto_logout()",timeoutMins);
atimer=setInterval("alert_idle()",timeout1Mins);
}
function alert_idle() {
var answer = confirm("Session About To Timeout\n\n You will be automatically logged out.\n Confirm to remain logged in.")
if (answer){
reset_interval();
}
else{
auto_logout();
}
}
function auto_logout() {
//this function will redirect the user to the logout script
window.open('self.location', '_self');
}
</script>
<script language="JavaScript">
function set_interval() {
//the interval 'timer' is set as soon as the page loads
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
itimer=setInterval("auto_logout()",timeoutMins);
}
function reset_interval() {
var timeoutMins = 1000 * 1 * 15; // 15 seconds
var timeout1Mins = 1000 * 1 * 13; // 13 seconds
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove 2. mouseclick 3. key press 4. scrolling
//first step: clear the existing timer
clearInterval(itimer);
clearInterval(atimer);
//second step: implement the timer again
itimer=setInterval("auto_logout()",timeoutMins);
}
function auto_logout() {
//this function will redirect the user to the logout script
window.open('self.location', '_self');
}
</script>
<body onLoad="set_interval(); document.form1.exp_dat.focus();" onKeyPress="reset_interval();" onmousemove="reset_interval();" onclick="reset_interval();" onscroll="reset_interval();">
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout("IdleWarning()", timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Show idle timeout warning dialog.
function IdleWarning() {
var answer = confirm("Session About To Timeout\n\n You will be automatically logged out.\n Confirm to remain logged in.")
if (answer){
ResetTimers();
}
else{
IdleTimeout();
}
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout(timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
函数集_interval(){
//页面加载后立即设置间隔“计时器”
var timeoutMins=1000*1*15;//15秒
var timeout1Mins=1000*1*13;//13秒
itimer=setInterval(“auto_logout()”,timeoutMins);
}
函数重置_间隔(){
var timeoutMins=1000*1*15;//15秒
var timeout1Mins=1000*1*13;//13秒
//重置计时器。在以下每个事件中重置计时器:
//1.鼠标移动2.鼠标单击3.按键4.滚动
//第一步:清除现有计时器
清除间隔(itimer);
间隙(atimer);
//第二步:再次执行计时器
itimer=setInterval(“auto_logout()”,timeoutMins);
}
函数自动_注销(){
//此函数将用户重定向到注销脚本
window.open('self.location','u self');
}
主体c
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout("IdleWarning()", timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Show idle timeout warning dialog.
function IdleWarning() {
var answer = confirm("Session About To Timeout\n\n You will be automatically logged out.\n Confirm to remain logged in.")
if (answer){
ResetTimers();
}
else{
IdleTimeout();
}
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
<script type="text/javascript">
// Set timeout variables.
var timoutWarning = 3000; // Display warning in 1Mins.
var timoutNow = 4000; // Timeout in 2 mins.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout(timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Logout the user and auto reload or use this window.open('http://www.YourPageAdress.com', '_self'); to auto load a page.
function IdleTimeout() {
window.open(self.location,'_top');
}
</script>
<body onload="StartTimers();" onmousemove="ResetTimers();" onKeyPress="ResetTimers();">
setInterval(function(){ location.reload(); }, 3000);
function reloadPage(expiryDurationMins) {
const lastInteraction = window.localStorage.getItem('lastinteraction')
if (!lastInteraction) return // no interaction recorded since page load
const inactiveDurationMins = (Date.now() - Number(lastInteraction)) / 60000
const pageExpired = inactiveDurationMins >= expiryDurationMins
if (pageExpired) window.location.reload()
}
const saveLastInteraction = () => window.localStorage.setItem('last', Date.now().toString())
window.addEventListener('beforeunload', () => window.localStorage.removeItem('lastinteraction'))
window.addEventListener('mousemove', saveLastInteraction)
window.addEventListener('keypress', saveLastInteraction)
const expiryDurationMins = 1
window.addEventListener('load', setInterval.bind(null, reloadPage.bind(null, expiryDurationMins), 1000))
let lastActionTaken = new Date().getTime();
function checkLastAction() {
let now = new Date().getTime();
if (now - lastActionTaken > 1000 * 60 * 60) window.location.reload();
else lastActionTaken = now;
}
window.addEventListener("mousemove", checkLastAction);
window.addEventListener("touchstart", checkLastAction);
window.addEventListener("keydown", checkLastAction);