javascript clearTimeout 无法在子页面上运行

标签 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 似乎是父窗口。 我被难住了。

最佳答案

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

相关文章:

javascript - 从 iframe 中访问自定义属性

javascript - 开发者工具中的 Vue 数组更新但不会重新渲染

javascript - 基于多个 CSS 类和来自多个复选框的输入显示/隐藏 tr 元素

javascript - 将纬度/经度转换为距离

javascript - 如何包含多个 Javascript 文件?

javascript - 带诺克的 Mocha 茶 : why am I getting timeout errors when doing cleanup in 'after()' and 'afterEach()' ?

javascript - 如何在 jquery 中动态更改属性值?

javascript - 如何使字段在 ionic + Angular 中居中对齐(在中心)

javascript - AngularJS - 在范围更改时使用 if else 语句更改变量值

javascript - 使用Javascript计算小数点前后的位数