//page time out handler
var pageTimeoutHandler = new function(){
//todo:this value may be got from server.
var defalutCheckTime = 301*1000; //s
var registerFlag = false;
var timeoutFlag = false;
var targetAddr = "";
var timeoutHandler = null;
var timeoutCallback = null;
function _setTimeoutFlag(){
$.getJSON(noCache("checkForTimeout.json"), null,function(jsonObj){
if(undefined != jsonObj){
if(undefined != jsonObj.redirectType){
if("timeout" == jsonObj.redirectType){
timeoutFlag = true;
targetAddr = jsonObj.address;
_setNoticeMsg();
}
}
}
});
}
function _clearCheckFlag(){
needCheckFlag = false;
timeoutFlag = false;
}
function _resetCheck(){
_clearCheckFlag();
if (null != timeoutHandler){
clearTimeout(timeoutHandler);
timeoutHandler = null;
}
timeoutHandler = setTimeout(_setTimeoutFlag, defalutCheckTime);
}
function _setNoticeMsg(){
window.parent.bootbox.alert($L('loginTimeout'), function() {
window.parent.window.location.href= 'login.html';
});
}
return{
registeCallBack:function(){
registerFlag = true;
_resetCheck();
},
resetCheck:function(){
if (!registerFlag){
return;
}
_resetCheck();
},
startCheck:function(){
if (!registerFlag){
return;
}
if (timeoutFlag){
//alert(getTagTextFromXmlDoc("timeout"));
topPageJump(targetAddr);
}
}
}
}
/* add random time for url,make sure the data is latest */
function noCache(url)
{
var delim = (url.indexOf("?") == -1) ? "?" : "&";
return url + delim + "timeStamp=" + new Date().getTime();
}
function pageProcessStart()
{
//for page timeout check;
if ((document.URL.indexOf("main.html") == -1) && (document.URL.indexOf("login.html") == -1))
{
pageTimeoutHandler.registeCallBack();
}
var pageTimeoutHandler = new function(){
//todo:this value may be got from server.
var defalutCheckTime = 301*1000; //s
var registerFlag = false;
var timeoutFlag = false;
var targetAddr = "";
var timeoutHandler = null;
var timeoutCallback = null;
function _setTimeoutFlag(){
$.getJSON(noCache("checkForTimeout.json"), null,function(jsonObj){
if(undefined != jsonObj){
if(undefined != jsonObj.redirectType){
if("timeout" == jsonObj.redirectType){
timeoutFlag = true;
targetAddr = jsonObj.address;
_setNoticeMsg();
}
}
}
});
}
function _clearCheckFlag(){
needCheckFlag = false;
timeoutFlag = false;
}
function _resetCheck(){
_clearCheckFlag();
if (null != timeoutHandler){
clearTimeout(timeoutHandler);
timeoutHandler = null;
}
timeoutHandler = setTimeout(_setTimeoutFlag, defalutCheckTime);
}
function _setNoticeMsg(){
window.parent.bootbox.alert($L('loginTimeout'), function() {
window.parent.window.location.href= 'login.html';
});
}
return{
registeCallBack:function(){
registerFlag = true;
_resetCheck();
},
resetCheck:function(){
if (!registerFlag){
return;
}
_resetCheck();
},
startCheck:function(){
if (!registerFlag){
return;
}
if (timeoutFlag){
//alert(getTagTextFromXmlDoc("timeout"));
topPageJump(targetAddr);
}
}
}
}
/* add random time for url,make sure the data is latest */
function noCache(url)
{
var delim = (url.indexOf("?") == -1) ? "?" : "&";
return url + delim + "timeStamp=" + new Date().getTime();
}
function pageProcessStart()
{
//for page timeout check;
if ((document.URL.indexOf("main.html") == -1) && (document.URL.indexOf("login.html") == -1))
{
pageTimeoutHandler.registeCallBack();
}
}
在文件加载完成时注册事件:
$(document).ready(function(){
pageProcessStart();
});
在封装的ajax中调用:
pageTimeoutHandler.startCheck();
$.ajax({
url:o.url,
data:o.data,
cache:false,
success : function(data) {
pageTimeoutHandler.resetCheck();
})
})