需要在iframe中从父级清除clearInterval,但无法正常工作 [英] Need to clearInterval in iframe from parent and it's not working

查看:86
本文介绍了需要在iframe中从父级清除clearInterval,但无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iframe中使用setInterval,我需要能够从父窗口中将其停止,并且它似乎无法正常工作.我正在为要从父级访问的setInterval事件分配一个全局变量('modelViewRefresh').

I'm using setInterval inside an iframe, and I need to be able to stop it from the parent window, and it doesn't seem to be working. I'm assigning a global variable ('modelViewRefresh') to the setInterval event to be accessed from the parent.

我在父级中使用此代码来尝试触发clearInterval,但似乎不起作用:

I'm using this code in the parent to try and trigger clearInterval, but it doesn't seem to be working:

var modelViewRefresh = document.getElementById(widget_id + '_iframe').contentWindow['modelViewRefresh'];
console.log("modelViewRefresh=" + modelViewRefresh); // this equals 2 for some reason
clearInterval( modelViewRefresh );

我什至直接尝试这样做:

I'm even trying this directly:

clearInterval( document.getElementById(widget_id + '_iframe').contentWindow['modelViewRefresh'] );

我已经在自己的窗口中打开了iframe,以验证是否正在创建变量(通过Firebug在DOM中查找).仅供参考,这不是一个不同的域...

I've opened the iframe in a window of its own to verify that the variable is being created (by looking in the DOM via Firebug). FYI it's not a different domain...

我主要在其他地方使用jQuery,但在某处阅读,最好在这种情况下使用原始" JavaScript.因此,我也欢迎使用jQuery解决方案.

I'm mainly using jQuery elsewhere, but read somewhere that it's best to use 'raw' javascript in this instance. So I'm open to a jQuery solution too.

有什么想法吗?也许我正在做错事情...

Any ideas? Maybe I am going about it wrong...

PS.我还希望能够再次从父母那里开始刷新".

PS. I'd also like to be able to start the 'refresh' again from the parent.

推荐答案

从外部调用时, clearInterval clearTimeout 都需要 iframe名称iframe.

Both clearInterval and clearTimeout require the iframe name when called from outside the iframe.

HTML示例:< iframe名称="iframe_0"></iframe>

JS示例:

iframe_0.clearInterval(modelViewRefresh)

要从父级重新启动: modelViewRefresh = iframe_0.setInterval(do_this,1000)

这篇关于需要在iframe中从父级清除clearInterval,但无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆