2012-02-02 47 views
0

这些是相关行。javascript clearTimeout不是从子页面工作的

// In the parent page I set the timer and attach its handle to the window 
window.warnTo = window.setTimeout("WarnTimeout();", 20000); 

// If I clear the timer in the same page it works great 
window.clearTimeout(window.warnTO); 

// In the child page launched with window.open, 
// I try to clear the timer and the timer still fires 
window.opener.clearTimeout(window.opener.warnTO); 

所有的变量似乎都被设置,而window.opener似乎是父窗口。 我很难过。

+0

通常情况下,你不应该为'setTimeout'或'setInterval'使用字符串参数,尽管在这种情况下,它似乎没问题。 – 2012-02-02 14:08:10

回答

2

您是否尝试使用opener属性访问该页面?

window.opener.clearTimeout(window.opener.warnTO); 

这里是我的示例代码来测试这个。我用的时间间隔,而不是超时给它一个更清晰的指示工作:

Opener.html

<script type="text/javascript"> 
window.onload = function(){ 
    document.getElementById('button').onclick = function(){ window.open("opened.html");} 
    window.x = setInterval("bla()",500); 
} 

function bla() 
{ 
    var obj = document.getElementById('output') 
    obj.innerHTML += "Bla"; 
} 
</script> 

<div id="output"></div> 

<button id="button">Open the new window</button> 

Opened.html

<script type="text/javascript"> 
    window.opener.clearInterval(window.opener.x); 
</script> 

UPDATE

正如你在评论中指出的那样,你在你提供的代码示例中也有一个错字。

warnTO更改为warnTo

+0

我不明白。这是他的确切代码行复制为+2? – jAndy 2012-02-02 13:39:37

+0

我写完这个答案后,他必须更新他的问题。 – 2012-02-02 13:41:55

+0

window.opener.location是父窗口中的URL,因此看起来没问题。 – 2012-02-02 13:49:44

相关问题