Note that there are some explanatory texts on larger screens.

plurals
  1. PORun Javascript Ajax calls only when the page is being viewed
    primarykey
    data
    text
    <p>I have a web Application running on a controller (Scarcely limited processing, memory and network bandwidth). The page is basically a simple HTML file full of LEDs that should be updated on an interval. On each interval, Javascript sends an Ajax request to the server and updates all the LEDs based on the reply. Now this works fine!</p> <p>The problem is when the user opens one of these pages and start browsing other stuff. For security and economical reasons, we don't want to update the page when the client is not seeing this page. This is the algorithm:</p> <p><img src="https://i.stack.imgur.com/qWfd9.png" alt="A flowchart of the page update algorithm"></p> <p>I developed this little test code to see how page events work (<a href="http://jsfiddle.net/MEDEE/1/" rel="nofollow noreferrer">see live on jsFiddle</a>):</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;script type="text/javascript"&gt; function m(msg){ document.getElementById("logplace").innerHTML+=msg+"&lt;br/&gt;"; } &lt;/script&gt; &lt;/head&gt; &lt;body onblur="m('Blur')" onerror="m('Error')" onfocus="m('Focus')" onload="m('Load')" onresize="m('Resize')" onunload="m('Unload')"&gt; &lt;span id="logplace"&gt;&lt;/span&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I need to page to be <strong>only</strong> updated if the user is currently viewing it. I searched the Stackoverflow and the following threads don't seem to be the answer:</p> <ul> <li><a href="https://stackoverflow.com/questions/8065846/keep-track-of-which-tab-was-being-viewed-when-page-is-refreshed">Keep track of which tab was being viewed when page is refreshed</a></li> <li><a href="https://stackoverflow.com/questions/5559347/how-to-run-multiple-ajax-calls-on-one-page">How to run multiple ajax calls on one page</a></li> <li><a href="https://stackoverflow.com/questions/7251461/function-repeating-when-page-not-being-viewed">Function repeating when page not being viewed</a></li> </ul> <p>PS. JQuery exists on the server as well. But if there is a simpler way, I prefer not to use JQuery.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload