473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronizing multiple AJAX calls in window.onload() event

I'm sure this q must have been asked before but I'm really struggling
to find the answer anywhere so have finally given up and will consult
the usenet community - hopefully there's someone out there who's seen
it all before and can help me out! I have a webpage which needs to
make some function calls after the page has loaded - won't go into
details surfice to say here is a code fragment:

function addLoadEvent(fu nc) {
var oldonload = window.onload;
if (typeof window.onload == 'function') {
window.onload = function() {
oldonload();
func();
}
} else {
window.onload = func;
}
}

addLoadEvent(my Function1);
addLoadEvent(my Function2);
addLoadEvent(my Function3);
...etc...

My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to
make sure of this? I've considered putting a hidden element in the
HTML and then changing it at the end of each function call and have
the onchange event trigger the next function call. But this seems very
hacky and overcomplicated - surely there must be a simpler and better
way....?

Cheers for any advice / suggestions,

Steve

May 18 '07 #1
5 6152
On 18 May, 22:17, steve.chamb...@ gmail.com wrote:
I'm sure this q must have been asked before but I'm really struggling
to find the answer anywhere so have finally given up and will consult
the usenet community - hopefully there's someone out there who's seen
it all before and can help me out! I have a webpage which needs to
make some function calls after the page has loaded - won't go into
details surfice to say here is a code fragment:

function addLoadEvent(fu nc) {
var oldonload = window.onload;
if (typeof window.onload == 'function') {
window.onload = function() {
oldonload();
func();
}
} else {
window.onload = func;
}
}

addLoadEvent(my Function1);
addLoadEvent(my Function2);
addLoadEvent(my Function3);
...etc...

My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to
make sure of this? I've considered putting a hidden element in the
HTML and then changing it at the end of each function call and have
the onchange event trigger the next function call. But this seems very
hacky and overcomplicated - surely there must be a simpler and better
way....?

Cheers for any advice / suggestions,

Steve
Could you use the readyState of one AJAX request to control the next
function?
May 18 '07 #2
On 18 May, 23:43, charles <char...@boisve rt.me.ukwrote:
On 18 May, 22:17, steve.chamb...@ gmail.com wrote:
I'm sure this q must have been asked before but I'm really struggling
to find the answer anywhere so have finally given up and will consult
the usenet community - hopefully there's someone out there who's seen
it all before and can help me out! I have a webpage which needs to
make some function calls after the page has loaded - won't go into
details surfice to say here is a code fragment:
function addLoadEvent(fu nc) {
var oldonload = window.onload;
if (typeof window.onload == 'function') {
window.onload = function() {
oldonload();
func();
}
} else {
window.onload = func;
}
}
addLoadEvent(my Function1);
addLoadEvent(my Function2);
addLoadEvent(my Function3);
...etc...
My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to
make sure of this? I've considered putting a hidden element in the
HTML and then changing it at the end of each function call and have
the onchange event trigger the next function call. But this seems very
hacky and overcomplicated - surely there must be a simpler and better
way....?
Cheers for any advice / suggestions,
Steve

Could you use the readyState of one AJAX request to control the next
function?
I don't think so, you're right in that the readyState tells me when
the AJAX call has completed but my problem is I need to know when the
resulting funcion has completed its processing (e.g. one of my
functions writes information to a database, which takes time. As
always, probably some more example code will help explain what I mean:

function function1() {
var ajax_connection = createRequest() ;
ajax_connection .open('get', 'getinfofromser ver.php');
ajax_connection .onreadystatech ange = function() {
doSomeDatabaseS tuff(ajax_conne ction);
}
ajax_connection .send(null);
}

function doSomeDatabaseS tuff(ajax_conne ction) {
// Only proceed if the state change is a completion
if (ajax_connectio n.readyState == ReadyStateEnum. COMPLETE) {
// Do some database stuff here
...
}
}

May 19 '07 #3
I have a webpage which needs to
make some function calls after the page has loaded - won't go into
details suffice to say here is a code fragment:
window.onload = function() {
myFunction1();
myFunction2();
myFunction3();
}
My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to
make sure of this? I've considered putting a hidden element in the
HTML and then changing it at the end of each function call and have
the onchange event trigger the next function call. But this seems very
hacky and overcomplicated - surely there must be a simpler and better
way....?
Could you use the readyState of one AJAX request to control the next
function?
I don't think so, you're right in that the readyState tells me when
the AJAX call has completed but my problem is I need to know when the
resulting funcion has completed its processing (e.g. one of my
functions writes information to a database, which takes time. As
always, probably some more example code will help explain what I mean:

function function1() {
var ajax_connection = createRequest() ;
ajax_connection .open('get', 'getinfofromser ver.php');
ajax_connection .onreadystatech ange = function() {
doSomeDatabaseS tuff(ajax_conne ction);
}
ajax_connection .send(null);
}

function doSomeDatabaseS tuff(ajax_conne ction) {
// Only proceed if the state change is a completion
if (ajax_connectio n.readyState == ReadyStateEnum. COMPLETE) {
// Do some database stuff here
...
}
}
In that case could you use the server response to control the timing?
Write the getinfofromserv er.php script so that when ajax_connection is
complete, you know all the processing on the server is complete. Then
you can call doSomeDatabaseS tuff safely.

Maybe I'm missing something simple, because I don't see how using a
form and onChange() would solve the problem.
May 19 '07 #4
On May 19, 1:27 am, charles <char...@boisve rt.me.ukwrote:
My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to
//Maybe something like (untested):

var set1 = new Array({url:'bs. pl',fx:function 1},
{url:'morebs.pl ',fx:function2} );
var set2 = new Array({url:'foo .pl',fx:functio n3},
{url:'bass.pl', fx:function4});
var actions = new Array(set1,set2 );

for(var i in fx) {
requestChain(fx[i])
}
function requestChain(ch ain) {

var set = chain.shift();
var req = AJAXRequest();

req.onreadystat echanged = function () {
//AJAX state checks...
if( set.fx() ) {
if(chain.length 0) new requestChain(ch ain);
}
};

req.open('get', set.url);
//etc....
}
May 20 '07 #5
On May 20, 3:14 am, "Skye Shaw!@#$" <skye.s...@gmai l.comwrote:
On May 19, 1:27 am, charles <char...@boisve rt.me.ukwrote:
My problem is that some of these myFunction#s include AJAX calls but
the calls depend on each other in such a way that myFunction2 will not
work unless myFunction1 has completed. So what is the "nicest" way to

//Maybe something like (untested):
<snip>
var actions = new Array(set1,set2 );

for(var i in fx) {
requestChain(fx[i])

}
oops, should be: for(var i in actions) {}
function requestChain(ch ain) {
if( set.fx() ) {
if(chain.length 0) new requestChain(ch ain);
new isn't necessary. Sorry, was a tad sleepy

May 20 '07 #6

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

Similar topics

6
4560
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way to tell if the window.onload event has already fired. I cannot edit the onload event handler itself, and my function can only exist in an external...
2
1508
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which contains a class that inherits from the System.Web.UI.Page. the class contains an override of the OnLoad event in which the common code is executed.
4
2456
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the value of an option in a select statement: <select id="usertypes" multiple="multiple"> <option value="0033">data1</option>
0
7178
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along the way. First, deep linking is not something that a completely AJAX web site should be able to do by it's very nature of everything being on one...
5
9429
by: lilOlMe | last post by:
Hi there! I'm developing some crazy Tab Control in .NET that uses JavaScript. A particular JavaScript method needs to be called during the window.onload event in order to initialize my Tab Control. The thing is that there can be more than one Tab Control on the page....and each one must be initialized during the window.onload event. ...
1
1831
by: violinandviola | last post by:
I have just put 4 different ajax bits on this page: http://jimpix.co.uk/default-ajax.asp The ajax spits out chunks of images / news content, and users can view the chunks via next / prev links. When I first view the page in Firefox, there is a short delay while the content first loads in the ajax sections. When I first view the page in...
1
2008
by: bizt | last post by:
Hi, I am having my first real attempt at an ajax class as so far Ive managed to build one that, once instatiated, will allow me to define which function it will call on completion then retrieves the contents from a server side script as AJAX generally does and process it using that function. This far Im quite pleased with it, however, I...
2
1648
by: burtonfigg | last post by:
I'm testing an ajax page - this works fine in Firefox: http://jimpix.co.uk/clients/a/ecards/defaultx.asp Click on any of the links on the right under the 'occassions' or 'others' headings, in Firefox, and thumbnails appear based on what you clicked on. Do the same in IE6, and it returns an error: Line: 71 Char: 9 Error: Unknown...
1
2756
by: petvampire | last post by:
not sure if some one got a answer to this little issue. Im built a page that calls information from a page called database-update.asp and puts it in to the div tag Reload this. What i am trying to do is make a pop up so if some one clicks the link shown it open a pop up window. When trying to do this nothing happens. Any ideas how i can get a pop...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.