问题描述
我有一个会话超时时间为15分钟的网站。在某些页面上,用户偶尔会花费超过15分钟的时间填写回复。在这种情况下保持活动状态的最佳解决方案是什么?
我已经在这些页面上使用 JQuery
所以可能ping一个服务器,但在什么事件?
后端是一个 Struts
on Tomcat
。
由于您不想更改网站的会话超时, / p>
在javascript中设置超时/间隔(
您可以维护一个变量 lastActivity
,该变量在每个文档mousemove或文档keydown上更新。如果自上次ping之后还有任何活动,则再次ping。
为了获得更复杂的结果,只有当事件的数量超过阈值时才能对事件进行计数并ping服务器。
基本的例子:
setInterval(function(){
$ .get('/ ImStillAlive.action');
},840000);通过基本检查打字活动:
$(function(){
var lastUpdate = 0;
var checkInterval = setInterval(function(){
if(new Date()。getTime() - lastUpdate> 840000){
clearInterval(checkInterval);
} else {
$ .get('/ ImStillAlive.action'); $ (
$ b $ 840000); // 14分钟* 60 * 1000
$(document).keydown(function(){
lastUpdate = new Date() .getTime();
});
});
I have a site with a session timeout of 15 minutes. On some pages a user occasionally spends longer than 15 minutes filling in a reply. What is the best solution to keep alive the session in this case?
I already use JQuery
on these pages, so possibly pinging a server, but on what events?
The backend is a Struts
on Tomcat
.
解决方案 Given you don't want to change the site's session timeout..
Set a timeout/interval (< 15min) event in javascript and decide what to do when the event triggers. If you want the session to be active as long as the page is open, then fine, keep pinging every < 15 min. But that's probably not what you want as a user leaving a public computer should get logged out at some point.
You could maintain a variable lastActivity
, which is updated on each document mousemove or document keydown. If there's been any activity since last ping, ping again.
To get more sophisticated, you could count the events and ping the server only if number of events > threshold at timeout.
The basic example:
setInterval(function(){
$.get('/ImStillAlive.action');
}, 840000); // 14 mins * 60 * 1000
With basic check for typing activity:
$(function(){
var lastUpdate = 0;
var checkInterval = setInterval(function(){
if(new Date().getTime() - lastUpdate > 840000){
clearInterval(checkInterval);
}else{
$.get('/ImStillAlive.action');
}
}, 840000); // 14 mins * 60 * 1000
$(document).keydown(function(){
lastUpdate = new Date().getTime();
});
});
这篇关于如何在论坛上发布用户会话时保持活跃状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!