4

Let's say I have a website where each user has a profile.

Now an user with an existing profile wants to edit it, so he clicks on a button "edit my profile".

Let's assume that the UI allowing the user to accomplish this is complex and takes some time to be rendered (100 ms).

The AJAX call to the server to retrieve the profile data takes 150 ms.

Once "edit my profile" is clicked I would proceed as following:

  • show a loading indicator
  • make the ajax call (150 ms)
  • as the ajax call returns build the UI and fill it with the data returned (100ms)

total time: 250ms

But... what if I would proceed in this other way:

  • start the ajax call
  • build the interface (with no content), maybe locking it with a modal popup on top which says "loading..."
  • when the ajax call returns unlock the interface and fill it

Does the browser start an AJAX call immediately, or does it wait for the current operation to finish?

Because if it does, this should be faster since it constructs the UI while making the AJAX call, taking 150 ms (overlapped UI construction + AJAX call) + let's say 20 ms to update the UI.

total time: 170 ms.

emanuele
  • 193
  • 1
  • 10
  • http://stackoverflow.com/a/11247064/1273830 – Prasanth Jul 29 '13 at 12:32
  • 3
    As long as you don't make your AJAX calls synchronous (in which case they wouldn't really be "AJAX" of course) they're done asynchronously. You can use tools like the Chrome network tab to show the time sequence of operations. – Pointy Jul 29 '13 at 12:32

1 Answers1

0

As the name suggests, AJAX-Calls are executed asynchronus. So while your request is on its way - other JS-Functions can be executed without a problem.

  • Thank you for your answer. So the browser starts the request immediately? Does it use another thread to make the request, while in another keeps executing the javascript? My question is about when the request starts. – emanuele Jul 29 '13 at 12:55
  • No javascript does not support multi-threading. Take a look at this article [link](http://www.sitepoint.com/multi-threading-javascript/) – David Hölkeskamp Jul 30 '13 at 07:02