Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>setInterval("getTemp()", 1000); </code></pre> <p>does not make sense at this line, and it might be the cause of your problems. This call causes <code>getTemp</code> to be called every second, starting from after this call as returned. In the <code>getTemp</code> function you are reusing the <code>xhr</code> variable, creating a new <code>XMLHttpRequest</code> instance each time. That alone is not necessarily a problem (aside from the <code>"Msxm12.XMLHTTP"</code> which appears to be wrong; should probably be <code>"Msxml2.XMLHTTP"</code>).</p> <p>But in each call of <code>getTemp</code> you are issuing a new request, <em>regardless</em> if the previous request has received a response (the third argument being <code>true</code> means <em>asynchronous</em> request-response handling). Consider this: The <code>readystatechange</code> listener might not have been called (or its <code>readyState == 4</code> part has not be executed) because the client is waiting for the server to respond. Now your one-second timer kicks in, calls <code>getTemp</code> <em>again</em> and thus <em>overwrites</em> the <code>xhr</code> value that is to be used in the listener code. There is a closure, <code>xhr</code> is a bound variable, and therefore there is a race condition already.</p> <p>Further, an HTTP client SHOULD NOT (per RFC 2616), and a well-designed Web browser will not, keep more than a specified number of persistent HTTP connections (default: 2 to 8) open to the same server or proxy. So the more complex the document, and the more resources need to be loaded in parallel, the more likely that this approach will fail.</p> <p>You should remove the <code>setInterval(…)</code> call and add a <code>getTemp()</code> call below the line where you update the <code>innerHTML</code> property (when the request was successful, <code>status == 200</code>), so that only then a new request is issued. You should at least wait for the <code>readyState</code> to become <code>4</code> (FINISHED) before you issue a new request. Since you want to wait one second before the next call, use <code>window.setTimeout("getTemp()", 1000)</code>.</p> <p>It should help your understanding if you pretty-print that code, that is, indent function body and block content with a reasonable amount of white-space, and add some white-space, for example between <code>)</code> and <code>{</code>. There are editors like Eclipse JavaScript Development Tools which can automate much of the code formatting.</p> <p>Afterwards you should learn to use a debugger (use Firebug for Firefox, newer versions of other browsers have one built-in) and set breakpoints in your code, particulary in <code>getTemp</code> and <code>parseHttpResponse</code>. Step through the code (caveat: <em>step over</em> <code>send()</code>, then continue so that execution halts at the breakpoint in <code>parseHttpResponse</code>) to see what is going on. Good luck.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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