我有一个文档"1.html",其中包含一个iframe,它加载"2.html"
在这两个html中,我都有以下代码:
window.onload=function(){
window.top.sesion_timer = setTimeout(function(){
window.top.f_cerrar_sesion();
},2000);
//si tocas tecla o clickeas reseteas contador
window.addEventListener('keydown',function(){ window.top.f_reset_sesion(); },false);
window.addEventListener('click',function(){ window.top.f_reset_sesion(); },false);
}在父文档中,我还有以下代码:
var sesion_timer;
function f_reset_sesion(){
clearTimeout(sesion_timer);
sesion_timer=setTimeout(f_cerrar_sesion,2000);
}
function f_cerrar_sesion(){
alert("cerrar");
}当我单击文档或iframe时,超时将重新加载,但间隔未被清除,因此最终调用函数f_cerrar_sesion,这是不会发生的事情。
发布于 2013-03-27 12:53:12
我终于发现了问题。
我必须首先检查文档是否是添加超时的绝对父文档,否则它将为每个iframe添加一个超时--因此,最终代码如下:
window.onload=function(){
//start the timeout if is the absolute parent
if(window.top===window.self) {
window.top.sesion_timer = window.top.setTimeout(function(){ window.top.f_cerrar_sesion(); },1200000);
}
//if keydown or click we reset the timeout calling f_reset_sesion(), where the timeout is
window.addEventListener('keydown',function(){ window.top.f_reset_sesion(); },false);
window.addEventListener('click',function(){ window.top.f_reset_sesion(); },false);
}https://stackoverflow.com/questions/15613544
复制相似问题