2

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.

mcknight
  • 607
  • 1
  • 5
  • 17
  • Is it that you want to close all concuring ajax calls or just stop sending them? Because a simple clearInterval would work for the later. – Salketer Oct 02 '12 at 14:33
  • But with a simple "clearinterval" in my main page i can't address javascript that was loaded in an iframe - or am i wrong? – mcknight Oct 02 '12 at 14:34
  • You are right, unless you affect it to "top" (your main window)... top.handler – Salketer Oct 02 '12 at 14:36

3 Answers3

5

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.

Kale McNaney
  • 457
  • 2
  • 4
  • Is there the possility to have a stop function which kills all Ajax-Request at once? – mcknight Oct 02 '12 at 14:37
  • See this: http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery – Aidas Bendoraitis Oct 02 '12 at 14:40
  • You have a timer running every 100ms that calls getStatus which I'm guessing sends an ajax request. Calling clearInterval will ensure that no NEW requests are sent, but you can't stop requests that have already been sent. FWIW, a roundtrip to your server will likely take ~100ms so your timer interval is too short in my opinion. I'd recommend lengthening the interval and call clearInterval when you want to stop sending requests. – Kale McNaney Oct 02 '12 at 14:41
  • @AidasBendoraitis - very cool. Wasn't aware of the abort function. – Kale McNaney Oct 02 '12 at 14:42
  • This looks nice. But I get the following error: window.parent.frame["frame1"] is undefined – mcknight Oct 02 '12 at 14:59
  • Give your frames the name attribute like `name="frame1"`. – Kale McNaney Oct 02 '12 at 15:10
  • Hi, i can try it at the end of the week. Thanks in advance for the answers. – mcknight Oct 03 '12 at 06:02
4

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 );
}
Anoop
  • 23,044
  • 10
  • 62
  • 76
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()
m1k3y3
  • 2,762
  • 8
  • 39
  • 68