HighDots Forums  

how to force sequential execution

Javascript JavaScript language (comp.lang.javascript)


Discuss how to force sequential execution in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Barlymasher
 
Posts: n/a

Default how to force sequential execution - 05-22-2008 , 04:04 PM






Hi all,

hope you can help. I am having a problem with a script I am running. I
am calling a function to retrieve data from an XML file, and then want
other functions to execute after that function is complete. Instead,
it seems to call the function and continue.

There are three sequential calls here. In the function getPoints()
there is an alert message (for testing purposes at this point, the end
goal is to have it return data). I would expect that entire function
to complete before going on to the next two alerts, however, what I am
seeing is it ends up being last. What do I need to do to make this
stuff go in the proper order?

var text2 = getPoints();
alert("This should be second");
alert('This should be third: ' + text2);

The full file and what is happening can be found here:
http://www.obinge.com/rides/newRoute.html

thanks for any help!

O

Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: how to force sequential execution - 05-22-2008 , 06:01 PM






Barlymasher wrote:
Quote:
hope you can help. I am having a problem with a script I am running. I
am calling a function to retrieve data from an XML file, and then want
other functions to execute after that function is complete. Instead,
it seems to call the function and continue.

There are three sequential calls here. In the function getPoints()
there is an alert message (for testing purposes at this point, the end
goal is to have it return data). I would expect that entire function
to complete before going on to the next two alerts, however, what I am
seeing is it ends up being last. What do I need to do to make this
stuff go in the proper order?

var text2 = getPoints();
alert("This should be second");
alert('This should be third: ' + text2);
There is a reason why it is called AJAX. getPoints() does not call the XHR
callback, but only defines it (assignment to onreadystatechange) and then
issues the request (send()). Therefore it returns before the request is
complete, and your alert() calls (that should be window.alert()) are
executed in between.

So your alert() (or whatever) calls need to be placed in the callback instead.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
  #3  
Old   
Barlymasher
 
Posts: n/a

Default Re: how to force sequential execution - 05-23-2008 , 07:40 AM



On May 22, 7:01 pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
Barlymasher wrote:
hope you can help. I am having a problem with a script I am running. I
am calling a function to retrieve data from an XML file, and then want
other functions to execute after that function is complete. Instead,
it seems to call the function and continue.

There are three sequential calls here. In the function getPoints()
there is an alert message (for testing purposes at this point, the end
goal is to have it return data). I would expect that entire function
to complete before going on to the next two alerts, however, what I am
seeing is it ends up being last. What do I need to do to make this
stuff go in the proper order?

var text2 = getPoints();
alert("This should be second");
alert('This should be third: ' + text2);

There is a reason why it is called AJAX. getPoints() does not call the XHR
callback, but only defines it (assignment to onreadystatechange) and then
issues the request (send()). Therefore it returns before the request is
complete, and your alert() calls (that should be window.alert()) are
executed in between.

So your alert() (or whatever) calls need to be placed in the callback instead.

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Thanks, that did the trick. I had done this before without using
callback functions (did not know about them anyway) and it worked -
must have been lucky on those.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.