Javascript 当此setTimeout()调用父对象中的另一个setTimeout()时,如何在iframe中运行setTimeout()回调函数?

Javascript 当此setTimeout()调用父对象中的另一个setTimeout()时,如何在iframe中运行setTimeout()回调函数?,javascript,Javascript,我的页面上有一个动态创建的iframe。像这样: var iframe = document.createElement('iframe'); iframe.setAttribute("id","myIframe"); iframe.src = "iframePage.html"; document.body.appendChild(iframe); 在“iframePage.html”中,我有运行在mainPage.html上的相同scriptfile.js。因此,我可以调用一个或另一个页面

我的页面上有一个动态创建的iframe。像这样:

var iframe = document.createElement('iframe');
iframe.setAttribute("id","myIframe");
iframe.src = "iframePage.html";
document.body.appendChild(iframe);
在“iframePage.html”中,我有运行在mainPage.html上的相同scriptfile.js。因此,我可以调用一个或另一个页面中的所有函数

在这个scriptfile.js中,我有一个由“iframePage.html”上的按钮调用的函数。该函数将在1秒内从DOM中删除iframe!就这样,

function closeAndRemoveIframe() {
   setTimeout(function() {
      var myIframe = window.parent.document.getElementById("myIframe");
      myIframe.parentNode.removeChild(myIframe);
   },1000);
}
function closeAndRemoveIframe() {
   setTimeout(function() {
      var myIframe = window.parent.document.getElementById("myIframe");
      myIframe.parentNode.removeChild(myIframe);
      myNewFunction();
   },1000);
}
嗯。 但我在这个函数中添加了对另一个函数的调用。但是这个函数包含一个setTimeout,它会产生3秒的延迟

就这样,

function closeAndRemoveIframe() {
   setTimeout(function() {
      var myIframe = window.parent.document.getElementById("myIframe");
      myIframe.parentNode.removeChild(myIframe);
   },1000);
}
function closeAndRemoveIframe() {
   setTimeout(function() {
      var myIframe = window.parent.document.getElementById("myIframe");
      myIframe.parentNode.removeChild(myIframe);
      myNewFunction();
   },1000);
}
问题是: 此新函数具有更长的运行时设置超时(3000毫秒)

iframe是调用“myNewFunction()”函数的框架。一旦它在1秒后从DOM中删除,新函数就不会出现,因为它有3秒的延迟

我找到的唯一解决方案是在第一个函数的setTimeout中设置相同的时间!但这不是令人满意的行为

那么,有没有办法解决这个问题而不必增加第一个函数的settimeout时间

附言:我用的是香草JS


非常感谢

您是否尝试过将
myNewFunction
调用移动到
removeChild
函数调用之前