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.
Thomas 'PointedEars' Lahn - 29 Nov 2005 22:19 GMT
> 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
Julian Turner - 30 Nov 2005 08:06 GMT
> Hi.
>
[quoted text clipped - 6 lines]
> (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
VK - 30 Nov 2005 11:02 GMT
> 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);
}
}
Jasen Betts - 30 Nov 2005 19:25 GMT
> 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