Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Javascript ajax call execution order

    Hi,
    I was wondering when a received ajax response gets executed. After searching I read about the Event loop, but I am not sure if I interpreted it correctly.

    Is it like this?:
    - Start execution
    - Ajax request is sent
    - proceeds execution
    - Ajax reply is received and buffered as a message
    - executing untill there is nothing to execute
    - Any triggered events are buffered in the message queue aswell.
    - nothing to execute so read next message and execute the associated function.

    Does this mean that when you send an ajax request and then before the ajax response is received 1000 interval triggers and 1000 click events have occured that the ajax response will be handled after the 1000 interval triggers and 1000 click event messages have been handled?

    I did a small test to test this and this seems to be the case, but i'd like to be sure and to know if there are exceptions or tricky things I should know about.

    Thanks!
    Stefan 1

  • #2
    New Coder
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hmm, and I guess with nested ajax requests you still can get a sort of race conditions right?

    Example:
    Code:
    this.streetName = "";
    this.houseNumber= "";
    this.loadStreetname( function(){
        this.loadHousenumber( function(){
            console.log(streetName + " " + houseNumber );
        }
    }, this ) );
    // the loadStreetname and loadHousenumber methods both make ajax requests
    // They set the streetName and houseNumber variables when the response is received. 
    // The functions passed as parameter are called on completion.
    The streetname may have been changed at the time the house number response is received and thus a wrong address may be printed. Offcourse this can be avoided by loading the entire address in one request, but I guess this is not always practical in other situations.

    In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,175
    Thanks
    23
    Thanked 601 Times in 600 Posts
    In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?
    You can control what is active on your HTML page.
    Read this for explanation of asynchronous - True or False?
    http://www.w3schools.com/ajax/ajax_x...quest_send.asp
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by stefan1 View Post
    The streetname may have been changed at the time the house number response is received and thus a wrong address may be printed. Offcourse this can be avoided by loading the entire address in one request, but I guess this is not always practical in other situations.

    In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?
    you can load them in two sequential async requests as well. if somethings changed on the back between those two, yeah, you'll be out of sync. But, those IOs happen just as fast sync as async, so even if you had used sync requests to load one after the other, you'd still be out of order. the server doesn't know the difference between a sync and async ajax call.


    this is the same exact reason why it's better to join your SQL tables and then SELECT them, rather than use a loop of queries resulting from an up-front SELECT. in order to reduce the risk of conflict, you want as whole of a snapshot as you need. same for ajax.


    being single-threaded gives JS a huge advantage in terms of program simplicity.
    using asyc IO means that most of the real CPU work happens off-thread; only the JS response handling/templating is done on that single JS thread; and it's usually 99.99% idle...

    of course, if you like threads, you can use them, even with ajax, even sync, without blocking your UI thread, using a webworker; new Worker(scriptUrl)...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    stefan1 (11-08-2013)

  • #5
    New Coder
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok so basically your ownly option for making sure that data that should form a consistent snapshot is to be retrieved in one go from the server.

    Another option would be to have some kind of lock like below but this doesn't really seem like an appropriate solution. Its not efficient and if you're not careful you may "forget" to unlock it.

    Code:
    var test = window.setTimeout( function(){
        if( locked == false ){
            window.clearInterval();
            locked = true;
            delayedFunction();
        }
    }, 1 );
    Anyway I was just wondering about the execution order which I now have a clear picture of.

    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •