这些是相关的行。
// 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 似乎是父窗口。 我被难住了。
最佳答案
您是否尝试过使用 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>
打开.html
<script type="text/javascript">
window.opener.clearInterval(window.opener.x);
</script>
更新
正如您在评论中指出的那样,您提供的代码示例中也存在拼写错误。
将 warnTO
更改为 warnTo
。
关于javascript clearTimeout 无法在子页面上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113214/