How to stop javascript ajax requests in inactive Iframe?

半世苍凉 提交于 2019-12-13 15:44:11

问题


I have made Website, where various other pages are built-in as "apps" with i frames:

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

In these apps, there is javascript code executed, among others very high-frequent HTTP-Requests.

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

app1 is polling information from a server very high frequently:

app1.php has Javascript in it::

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

When i now click on app2, the other app pops up, and i want to stop the javascript, which was loaded in app1.php, due to performance reasons.

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

How can i do that? Do you have any ideas?

Thanks in advance.


回答1:


From within frame2 you can try window.parent.frame["frame1"].stop() and in frame 1 you define stop() as a function which clears your interval.




回答2:


Since all iframes have there own window.You can use window.onfocus. window.onblur. put code inside each iframe. jsfiddle

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

}
window.onblur = function(){

  clearInterval(Handler );
}



回答3:


make sure both iframes are are hidden on the page load

once you open them with

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

add javascript code to load the content with

$('#app2button').html().load()


来源:https://stackoverflow.com/questions/12692307/how-to-stop-javascript-ajax-requests-in-inactive-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!