javascript clearTimeout 在子页面中不起作用

发布于 2025-01-02 00:13:36 字数 472 浏览 0 评论 0原文

这些是相关的行。

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

These are the relavent lines.

// 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);

All variables appear to be set and window.opener appears to be the parent window.
I'm stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2025-01-09 00:13:36

您是否尝试过使用 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>

UPDATE

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

warnTO 更改为 warnTo

Have you tried to access the page using the opener property?

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

Here's my sample code for testing this. I use Interval rather than Timeout to give a clearer indication of it working:

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

As you pointed out in the comments, you've also got a typo in the code sample you've provided.

Change warnTO to warnTo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文