Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Javascript has an event queue. It means that it handles ALL events (user-triggered events, setTimeout events, ajax returns events) <strong>one by one, as they come</strong>.</p> <p><strong>You cannot make assumptions on the execution order, this is definitely not the right way to go.</strong> That doesn't mean that you can't do synchronization. For instance:</p> <pre><code>function processURLs() { var url1 = "http://www.url1.com/"; var url2 = "http://www.url2.com/"; var data1 = null; var data2 = null; ajaxFunction(url1, params, function(data){ data1 = data; if( data2 !== null ) { process(data1, data2); } }); ajaxFunction(url2, params, function(data){ data2 = data; if( data1 !== null ) { process(data1, data2); } }); } </code></pre> <p>You said that javascript is single-thread. That's right. That thread keeps looping and pops the events from this queue when there's events to process.</p> <p>Even if the calls finished exactly at the same time and same timestamp, there will be one that will be enqueued to this event queue before the other (because your system will transmit the messages to the javascript process in some order).</p> <p>If you want to know <strong>how javascript timer works</strong> with that event queue, i deeply recommend the reading of <a href="http://ejohn.org/blog/how-javascript-timers-work/" rel="nofollow"><strong>John Resig's blog post about it</strong></a></p> <p>If you want more information about <strong>how network events are passed to your browser</strong> (javascript), you should learn about the <a href="http://en.wikipedia.org/wiki/OSI_model" rel="nofollow"><strong>OSI Model</strong></a>. </p> <p>For instance, your browser is in the OSI layer 7 (Application), but the order of network events will be decided below (layers 3 to 6).</p> <p>So to sum up the answer: nobody can tell you changeMe will be url1 or url2. <strong>Javascript won't decide the order here, it will be decided in deeper layers</strong> (your network card, your operating system, etc).</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. 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