473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how does ajax affect execution of code

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
Aug 18 '08 #1
7 1561
Andrew Poulos wrote:
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).
--
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/
Aug 18 '08 #2
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
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Aug 18 '08 #3
Gregor Kofler wrote:
Andrew Poulos meinte:
>If I have code that looks like this

ajax = function(str) {

var ajax = ... might be smarter.
If ajax was not declared earlier.
> 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?
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

Aug 18 '08 #4
On Aug 18, 10:18 am, Andrew Poulos wrote:
Gregor Kofler wrote:
>Andrew Poulos meinte:
>>If I have code that looks like this
>>ajax = function(str) {
>var ajax = ... might be smarter.

If ajax was not declared earlier.
>> 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?

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.
Aug 18 '08 #5
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
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Aug 18 '08 #6
Gregor Kofler wrote:
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 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 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.
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
Aug 19 '08 #7
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
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Aug 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
9
by: dougloj | last post by:
Hi. I have an ASP.NET application written in C#. In part of my application, I want to use JavaScript OnClick event function to update a textbox with a string generated asynchronously on the...
8
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
I am new to AJAX. I am applying AJAX to a current web solution to get the "instant behaviour". On my main page I have two sets of criteria: Specific and Wide. Each set is placed in a View...
5
by: pbd22 | last post by:
Hi. I am having a hard time figuring this one out... I have a sign in page. in my sign in logic, the successful login uses forms authentication and assigns an HttpCookie for the site-wide...
3
by: Mukesh | last post by:
Hi all I have Created an web application using VS 2005, asp.net2.0, Ajax Extensions 1.0, Ent Lib 3.1 , MS sql Server 2005, ajax Control tool kit Version 1.0.10618.0
12
by: lali.b97 | last post by:
Somewhere in a tutorial i read that if statement has performance overheads as code within the if statement cannot take benefit of pipeling of microprocessor and also that the compiler cannot...
2
by: =?Utf-8?B?REo=?= | last post by:
I have a peculiar problem here that I did not have until I migrated from ASP.NET 2.0 to 3.5. I use a master page for my application. Because the master page uses update panels I have the...
29
by: zalek | last post by:
I am writing application with Ajax in sync mode - xmlHttp.open("GET", url, false). I noticed that in FireFox handler doesn't starts. It starts when I use xmlHttp.open("GET", url,true). I need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.