`
ylz4647
  • 浏览: 48850 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

Web系统超时监听

阅读更多
本文转载于:http://haoi77.iteye.com/blog/197101


在Web系统中有时会有监听超时的需要。当用户一定的时间内不在当前Web系统进行任何操作时,系统需要做出反应。例如弹出提示对话框,或是将用户退出登录状态。

     由于用户在界面的操作往往不会与后台进行交互,因此在后台采用SessionListner的监  听方式无法满足该需求。这里建议使用javascript来实现此项功能。单页面与框架页面的超时监听分开介绍:
   
1、单页面超时监听
  

超时监听的javascript程序如下:

var maxTime = 300;  //最大计时值为300S
var timeOutValue = maxTime;

/**
* 计时监听程序,在页面初始化时调起即可
*
*/
function timeListener()
{
   timeOutValue = timeOutValue - 1;
   if(timeOutValue<=0)
   {
       timeOutValue = maxTime;
       alert("尊敬的用户,您已经"+ maxTime/60+" 分钟没有操作...");      
   }
   setTimeout("timeListener()",1000);   //每隔一秒监听一次  
}

对于单页面的超时监听,在页面初始化时调用timeListener就能搞定( <body onload="timeListener()"> )。


2、框架页面超时监听

  但是对于框架页面,如何处理?例如需要对下面一个简单的框架页面进行监听:

<frameset rows="80,*" cols="*" frameborder="no" border="0" framespacing="0">
  <frame  src="top.htm" name="topFrame" scrolling="No" noresize="noresize" id="topFrame" />
  <frameset cols="80,*" frameborder="no" border="0" framespacing="0">
    <frame  src="left.htm" name="leftFrame" scrolling="No" noresize="noresize" id="leftFrame" />
    <frame  src="main.htm" name="mainFrame" id="mainFrame" />
  </frameset>
</frameset>

    该框架有3个frame,当用户在任意一个框架区域发生操作都能够被监听到。如何实现?
这里推荐借助JQuery库的来实现,JQuery详细的使用方法请登录http://jquery.com/

    具体实现如下:

    在top.htm中编写"计时监听"程序和"监听框架各个区域动作"程序:

<script type="text/javascript" src="../js/jquery.js"></script>   //导入jQuery库文件
<script type="text/javascript">
var maxTime = 300;  //最大计时值为300S
var timeOutValue = maxTime;

function timeListener()
{
   timeOutValue = timeOutValue - 1;
   if(timeOutValue<=0)
   {
       timeOutValue = maxTime;
       alert("尊敬的用户,您已经"+ maxTime/60 + " 分钟没有操作...");      
   }
   setTimeout("timeListener()",1000);   //每隔一秒监听一次  
}

/**
*页面初始化时加载计时监听程序
*/
$(document).ready(function()
{
   timeListener();
});


/**
*监听topFrame区域的各种动作,一旦有动作发生,就重新计时
/
$(window.parent.topFrame).blur(function()
{
   timeOutValue = maxTime;
}).click(function()
{
   timeOutValue = maxTime;
}).dblclick(function()
{
   timeOutValue = maxTime;
}).focus(function()
{
   timeOutValue = maxTime;
}).keydown(function()
{
   timeOutValue = maxTime;
}).keypress(function()
{
   timeOutValue = maxTime;
}).mousedown(function()
{
   timeOutValue = maxTime;
}).mousemove(function()
{
   timeOutValue = maxTime;
}).mouseover(function()
{
   timeOutValue = maxTime;
}).scroll(function()
{
   timeOutValue = maxTime;
});


/**
*监听leftFrame区域的各种动作,一旦有动作发生,就重新计时
/
$(window.parent.leftFrame).blur(function()
{
   timeOutValue = maxTime;
}).click(function()
{
   timeOutValue = maxTime;
}).dblclick(function()
{
   timeOutValue = maxTime;
}).focus(function()
{
   timeOutValue = maxTime;
}).keydown(function()
{
   timeOutValue = maxTime;
}).keypress(function()
{
   timeOutValue = maxTime;
}).mousedown(function()
{
   timeOutValue = maxTime;
}).mousemove(function()
{
   timeOutValue = maxTime;
}).mouseover(function()
{
   timeOutValue = maxTime;
}).scroll(function()
{
   timeOutValue = maxTime;
});


/**
*监听mainFrame区域的各种动作,一旦有动作发生,就重新计时
/
$(window.parent.mainFrame).blur(function()
{
   timeOutValue = maxTime;
}).click(function()
{
   timeOutValue = maxTime;
}).dblclick(function()
{
   timeOutValue = maxTime;
}).focus(function()
{
   timeOutValue = maxTime;
}).keydown(function()
{
   timeOutValue = maxTime;
}).keypress(function()
{
   timeOutValue = maxTime;
}).mousedown(function()
{
   timeOutValue = maxTime;
}).mousemove(function()
{
   timeOutValue = maxTime;
}).mouseover(function()
{
   timeOutValue = maxTime;
}).scroll(function()
{
   timeOutValue = maxTime;
});


</script>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics