javascript clearTimeout无法从子页面运行

时间:2012-02-02 13:32:42

标签: javascript

这些是相关的行。

// 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似乎是父窗口。 我很难过。

1 个答案:

答案 0 :(得分:2)

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

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

这是我测试它的示例代码。我使用Interval而不是Timeout来更清楚地表明它的工作原理:

<强> 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>

<强>更新

正如您在评论中指出的那样,您提供的代码示例中也有拼写错误。

warnTO更改为warnTo