By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Asyncronous javascript component or thread emulation?

P: n/a
Hi.

I'm working on a project where a javascript program will get into
the middle of some very complicated operation and then decide
it needs to use to download an xml file from a server.

Because it's in the middle of something potentially complicated
I need to down the file synchronously
(without interrupting the flow of control), but I'd like
to make sure that the browser doesn't freeze up.

In another programming environment I would run the javascript
component in a thread -- is there any portable and safe way to
emulate this in javascript on recent browsers? My initial research
seems to say "no" but I'm not sure if I'm looking at up to date
information.

Please inform. Thanks! -- Aaron Watters

===

Horse: the other red meat.

Nov 29 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
aa***************@yahoo.com wrote:
Because it's in the middle of something potentially complicated
I need to down the file synchronously (without interrupting the
flow of control), but I'd like to make sure that the browser doesn't
freeze up.
I do not think it is possible to resolve that contradiction.
You have to decide what is more important to your task.
In another programming environment I would run the javascript
component in a thread -- is there any portable and safe way to
emulate this in javascript on recent browsers?


No, ECMAScript implementations like JavaScript 1.5+ (Mozilla/5.0)
and JScript 5.5+ (IE/5.5+) are single-threaded.
PointedEars
Nov 29 '05 #2

P: n/a

aa***************@yahoo.com wrote:
Hi.

I'm working on a project where a javascript program will get into
the middle of some very complicated operation and then decide
it needs to use to download an xml file from a server.

Because it's in the middle of something potentially complicated
I need to down the file synchronously
(without interrupting the flow of control), but I'd like
to make sure that the browser doesn't freeze up.

[snip]

There are only two hacks I can think of which might help:-

1. If you are using the HTTPRequest object to download the XML file,
this can be set to synchronous or asynchronous (i.e. the function that
called the HTTPRequest can still keep running) download.

2. You could spawn a pop-up window/modeless dialogue box or
IFRAME(?) to do the download, as these are in some implementations
effectively a separate thread.

For instance, I use a pop-up window to provide a progress bar for a
function in the parent window. The parent window function can send an
update to the pop-up window without interrupting the parent window
function.

Regards

Julian Turner

Nov 30 '05 #3

P: n/a
VK

aa***************@yahoo.com wrote:
Because it's in the middle of something potentially complicated
I need to down the file synchronously
(without interrupting the flow of control), but I'd like
to make sure that the browser doesn't freeze up.


These are mutually exceptive requirements: the whole idea of
synchronized method (not in JavaScript only) is that the program flow
doesn't proceed until the method has returned the result. A
synchronized method that allows the sequent statements executions w/o
waiting for results is a non-sense (it is not synchronized then).

What you really want I guess is to prevent data access until new data
is ready. This is usually achieved by setting and checking flags.

Pseudo-code:

if (data update needed) {
glbFlagDataState = 'updating';
make XMLHttpRequest with callback function itsDone;
}

function itsDone() {
glbFlagDataState = 'ready';
}

function usingData() {
if (glbFlagDataState == 'ready') {
// do what you want
}
else {
// try your luck on the next system tick:
setTimeout(usingData);
}
}

Nov 30 '05 #4

P: n/a
On 2005-11-29, aa***************@yahoo.com <aa***************@yahoo.com> wrote:
Because it's in the middle of something potentially complicated
I need to down the file synchronously
(without interrupting the flow of control), but I'd like
to make sure that the browser doesn't freeze up.
install an infinite bandwidth connection between the cient machine and the
server. :) , live with the delay, or figure out how to do it asynchronously.
In another programming environment I would run the javascript
component in a thread -- is there any portable and safe way to
emulate this in javascript on recent browsers?


possibly.

Bye.
Jasen
Dec 1 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.