2

我制作了网站,其中各种其他页面内置为带有 i 框架的“应用程序”:

<iframe id="frame1" src="./app1.php">
<iframe id="frame2" src="./app2.php">

在这些应用程序中,执行了 javascript 代码,其中包括非常高频的 HTTP 请求。

$('#app1button').click(function () {
     //Here i have to stop the HTTP-Requests which where fired from the JS of App2
     hideAllApps();
     showApp(app1);
});

app1 非常频繁地从服务器轮询信息:

app1.php 中有 Javascript::

 Handler = window.setInterval(getStatus, 100); //start status messages

当我现在单击 app2 时,会弹出另一个应用程序,由于性能原因,我想停止在 app1.php 中加载的 javascript。

$('#app2button').click(function () {
    //Here i have to stop the HTTP-Requests which where fired from the JS of App1
    hideAllApps();
    showApp(app2);
});

我怎样才能做到这一点?你有什么想法?

提前致谢。

4

3 回答 3

5

在第 2 帧中,您可以尝试window.parent.frame["frame1"].stop()在第 1 帧中将 stop() 定义为清除间隔的函数。

于 2012-10-02T14:33:56.053 回答
4

由于所有 iframe 都有自己的窗口。您可以使用window.onfocus. window.onblur. 将代码放在每个 iframe 中。jsfiddle

var Handler ;
window.onfocus = function(){
         Handler = window.setInterval(getStatus, 100); //start status messages

}
window.onblur = function(){

  clearInterval(Handler );
}
于 2012-10-02T14:47:25.757 回答
3

确保两个 iframe 在页面加载时都被隐藏

一旦你打开它们

$('#app2button').click(function () { 

添加javascript代码以加载内容

$('#app2button').html().load()
于 2012-10-02T14:35:32.410 回答