0

If I have a JS function as follows;

function testFn()
{
x.ajaxMethod(param1,JScallBackFunction); //Please do not worry about the syntax..this just indicates an external method call
alert("Line after ajaxMethod");
}

The ajaxMethod(), lets say is some kind of method defined in an external Java file (so it can be through DWR or anything) which returns some data...Point is it takes some time to execute this line of code...

Now my question is when will the alert on next line get fired (i.e. alert("Line after ajaxMethod");)

  1. Will it wait for these 2 things to complete (ajaxMethod execution as well as JScallBackFunction)
    OR
  2. It will be fired immediately without waiting for any of the above 2 things to complete ?

Also if you could guide in general about the JavaScript method flow execution, that will be great.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
copenndthagen
  • 49,230
  • 102
  • 290
  • 442

3 Answers3

3

It depends. Ajax calls are usually asynchronous which means the execution of code will not be paused until the asynchronous function returns. Therefore the alert will be executed immediately.

Asynchronous functions in javascript are usually to do with Ajax and loading something from a remote server. If you do wish to force JavaScript to wait while loading that content then you can set a flag for the XMLHTTPRequest object.

this is a good question to read: When is JavaScript synchronous?

Community
  • 1
  • 1
Roman
  • 10,309
  • 17
  • 66
  • 101
2

it will fire immediately after the ajax call. if you want it to wait put it in the callback function.

edit: a method that defines a callback is essentially this:

function(param1, callback) {
   // do stuff
   callback(); // execute callback
}
David Wick
  • 7,055
  • 2
  • 36
  • 38
0

First, when you say

The ajaxMethod(), lets say is some kind of method defined in an external Java file

I suppose you really mean external JavaScript file.

When you send an Ajax request, you ask the browser to send a request to the server for you. This request on the server may take sometime and you don't want to "wait" on it. (This is the whole idea of Async requests - stuff in the background).
So you tell the browser, here send this request to the server. Don't bother me unless the server responds, and once the server responds (we have a "response"), call this method. This is called callback. The method is called at a later point, when the response comes.

So the statement

x.ajaxMethod(param1,JScallBackFunction);

(assuming that it does gets a XmlHttpRequest, initializes it and calls the send method on it*) actually does two things:

  1. Sends the Ajax request
  2. Registers a call back function that will be called when the server responds (when we have an response). JScallBackFunction will be called when there is an response from the server.

But since this is an asynchronous request, the browser does not "wait" instead it continues to the next statement (if there is one) after the Ajax call and executes it.

So, alert("Line after ajaxMethod"); will be executed immediately.

*If this does not make any sense for you, this is how an Ajax request is actually "created" and "sent". This article may help you understand.

Nivas
  • 18,126
  • 4
  • 62
  • 76
  • Thx a lot for the detailed reply...2 things; 1). ajaxMethod() is actually defined in a .java file and through DWR, I am calling it in JS...Not sure if pure AJAX has the ability to do this thing OR we always need DWR or something to call a specific Java method from within JS. 2) If I simply make a "sync" AJAX request using obj.open("GET", url, false); will in that case the next line of code will wait for the complete execution of Java method and JS callback before firing the alert..I understand AJAX is meant to be async..but just to understand... – copenndthagen Jun 21 '11 at 08:38
  • 1. yes, you need something like DWR for calling a Java method within JS in the way you have shown. "Traditionally" the JS method would submit a request to a server and the server would forward this to a servlet. With DWR this is made easier. 2. If you make a sync Ajax request, all other Javascript will "wait" on this request to complete. So in this case the call back will be called and then the alert will be called. – Nivas Jun 21 '11 at 10:49