2013-03-25 53 views
0

我有一个文件“1.HTML”与加载“2.HTML”clearTimeout()不工作

在两个HTML的iframe我有这样的代码:

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超时重装但interva我没有清除,所以最后函数f_cerrar_sesion被称为,这是不会发生的事情

+1

就在这里注意:不要使用警报来进行这种调试。这是一种阻止方法,所以在警报等待响应时不会有其他执行。使用'console.log()'并打开你的JavaScript控制台。我不是100%确定这个拦截提醒对您的计时器有什么影响。 – Lix 2013-03-25 11:17:06

+0

我认为你应该使用'window.parent'而不是'window.top' – 2013-03-25 11:20:00

+0

是的,我这样做,但事情是,我已经覆盖了项目中的警报功能 – jsertx 2013-03-25 11:20:42

回答

0

我终于找到了问题。

我先来检查,如果该文件是绝对父文档添加超时,其他的方式它将每个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); 
}