how does ajax affect execution of code

A

Andrew Poulos

If I have code that looks like this

ajax = function(str) {
var val = "";
// do some stuff here
return val;
};

var foo = ajax("string");


Does execution continue before a value is assigned to foo?

Andrew Poulos
 
D

David Dorward

Andrew said:
If I have code that looks like this

ajax = function(str) {
var val = "";
// do some stuff here
return val;
};

var foo = ajax("string");

This doesn't seem to have anthing to do with Ajax aside from your choice of
variable name.
Does execution continue before a value is assigned to foo?

Continue from where?

The current code assigns a function to a variable (declared without var)
called 'ajax'. It immediately calls that function passing in the
argument "string". Next it assigns an empty string to a variable 'val', and
then returns that variable (assinging it to a variable called 'foo').

There is nothing odd about the timing of this.

(If XMLHttpRequest was involved, then the timing might get interesting, but
it isn't).
 
G

Gregor Kofler

Andrew Poulos meinte:
If I have code that looks like this

ajax = function(str) {

var ajax = ... might be smarter.
var val = "";
// do some stuff here
return val;
};

var foo = ajax("string");


Does execution continue before a value is assigned to foo?

In this very example? No.

Do you know what ajax or - to be more precise - "XHR" is?

Gregor
 
A

Andrew Poulos

Gregor said:
Andrew Poulos meinte:

var ajax = ... might be smarter.

If ajax was not declared earlier.
In this very example? No.

Do you know what ajax or - to be more precise - "XHR" is?
Sorry I was saving space by only including what I thought was relevant
code. Please assume that within the function assigned to the variable
"ajax" are all the relevant XMLHttpRequest bits and pieces.

If I reword the question to, while we're waiting for something to return
from an XMLHttpRequest does execution of the javascript code continue?

If it does what happens when the event onreadystatechange has a
readyState equal to 4? Does execution stop whenever its up to and run
the code under the onreadystatechange function?

Andrew Poulos
 
H

Henry

If ajax was not declared earlier.




Sorry I was saving space by only including what I thought was
relevant code. Please assume that within the function assigned
to the variable "ajax" are all the relevant XMLHttpRequest
bits and pieces.

So should we be assuming that the XML HTML requests are being made
synchronously or asynchronously?
If I reword the question to, while we're waiting for something
to return from an XMLHttpRequest does execution of the javascript
code continue?

To which the answer is yes or no, depending on how you make the XML
HTTP request.
If it does what happens when the event onreadystatechange
has a readyState equal to 4?

If anything is going to happen when the readyState equals 4 then "it"
must do whatever is done.
Does execution stop whenever its up to and run
the code under the onreadystatechange function?

Execution stops and code runs? That seems a bit contradictory. Are you
asking whether events interrupt already executing code? They don't.
 
G

Gregor Kofler

Andrew Poulos meinte:
Sorry I was saving space by only including what I thought was relevant
code. Please assume that within the function assigned to the variable
"ajax" are all the relevant XMLHttpRequest bits and pieces.

Ah a guessing game. Synchronous calls or asynchronous? In the latter
case: Once the request is sent, the skript moves on - therefore you need
a callback function to access the information returned from the server.
If I reword the question to, while we're waiting for something to return
from an XMLHttpRequest does execution of the javascript code continue?

If I'd have to wait: What would this XHR fuss all be good for? You (or
rather your script) normally doesn't wait. See above.
If it does what happens when the event onreadystatechange has a
readyState equal to 4? Does execution stop whenever its up to and run
the code under the onreadystatechange function?

Depends on what you mean by "execution stops". If it helps: JS is single
threaded.

Gregor
 
A

Andrew Poulos

Gregor said:
Andrew Poulos meinte:


Ah a guessing game. Synchronous calls or asynchronous? In the latter
case: Once the request is sent, the script moves on - therefore you need
a callback function to access the information returned from the server.

Thanks I didn't know they could be synchronous.
If I'd have to wait: What would this XHR fuss all be good for? You (or
rather your script) normally doesn't wait. See above.


Depends on what you mean by "execution stops". If it helps: JS is single
threaded.

Sorry, its obvious that I'm having trouble understanding ajax concepts.

I can get a value returned by a server using ajax. How do you handle the
situation where subsequent ajax calls are dependent upon the value
returned from previous calls? As I don't know when those values will return.

Andrew Poulos
 
G

Gregor Kofler

Andrew Poulos meinte:
I can get a value returned by a server using ajax. How do you handle the
situation where subsequent ajax calls are dependent upon the value
returned from previous calls? As I don't know when those values will
return.

Those subsequent XHR calls are triggered in the response callback function.

Pseudo:

xhr.onreadystatechange = function() {
if (xhr.readystate === 4) {
foo(xhr.responseText);
}
}
var foo = function(responseText) {
trigger another XHR request;
}


Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top