473,387 Members | 1,592 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,387 software developers and data experts.

Threads in Javascript

I have a big, complicated Javascript program that's doing some rather
complex things. One of the things it really has to do is to wait for the
browser to load something.

Because Javascript is all event-driven, the closest I seem to be able to
come is this:

loadMyFile("http://...insert url here...", function () {
alert("the file has loaded!");
});

That is, loadMyFile() executes asynchronously but registers a callback to
execute once the thing's loaded.

Unfortunately, while this works, it's a pig to program for. What I'd like is
for loadMyFile() to be synchronous: that is, it doesn't return until the
file has completely loaded.

(And I want to wait for other things apart from the file loading.)

It would appear that in order to do this I'm going to need multiple threads
of execution, somehow. I need to be able to have my main thread block, and
get woken up later by another thread. Is this at all possible?

Some experimentation would appear to suggest that all Javascript code on a
particular page, *including* code running in embedded iframes, appears to
execute in a single thread. Javascript on another page may or may not
execute in a different thread on my browser, but that's not terribly
important because I can't communicate across pages anyway.

Any suggestions?

I'm currently targeting IE6 and Firefox; I'd prefer standards-compliant
solutions, if any exist, but am willing to resort to hackery if I have
to...

--
+- David Given --McQ-+ "There is // One art // No more // No less // To
| dg@cowlark.com | do // All things // With art // Lessness." --- Piet
| (dg@tao-group.com) | Hein
+- www.cowlark.com --+

Jul 23 '05 #1
5 10085
> I have a big, complicated Javascript program that's doing some rather
complex things. One of the things it really has to do is to wait for the
browser to load something.

Because Javascript is all event-driven, the closest I seem to be able to
come is this:

loadMyFile("http://...insert url here...", function () {
alert("the file has loaded!");
});

That is, loadMyFile() executes asynchronously but registers a callback to
execute once the thing's loaded.

Unfortunately, while this works, it's a pig to program for. What I'd like is
for loadMyFile() to be synchronous: that is, it doesn't return until the
file has completely loaded.

(And I want to wait for other things apart from the file loading.)

It would appear that in order to do this I'm going to need multiple threads
of execution, somehow. I need to be able to have my main thread block, and
get woken up later by another thread. Is this at all possible?

Some experimentation would appear to suggest that all Javascript code on a
particular page, *including* code running in embedded iframes, appears to
execute in a single thread. Javascript on another page may or may not
execute in a different thread on my browser, but that's not terribly
important because I can't communicate across pages anyway.

Any suggestions?


Threads are evil. They should not be used at the application level and
never at the scripting level. Learn how to use events. That is the right
way to do this.

http://www.crockford.com/javascript
Jul 23 '05 #2


David Given wrote:
I have a big, complicated Javascript program that's doing some rather
complex things. One of the things it really has to do is to wait for the
browser to load something.

Because Javascript is all event-driven, the closest I seem to be able to
come is this:

loadMyFile("http://...insert url here...", function () {
alert("the file has loaded!");
});

That is, loadMyFile() executes asynchronously but registers a callback to
execute once the thing's loaded.

Unfortunately, while this works, it's a pig to program for. What I'd like is
for loadMyFile() to be synchronous: that is, it doesn't return until the
file has completely loaded.


You haven't shown us what loadMyFile looks like but in browsers I know I
think script runs in the same thread as the GUI meaning if you have a
script being blocked by waiting for some resource to be loaded the
browser GUI is locked too.
Therefore synchronous loading is usually not a good idea.

If you are using XMLHttpRequest (Mozilla, Netscape, latest Safari and
Opera) or Microsoft.XMLHTTP (IE/Win) then you can however use the third
argument to the open method set to false to have a synchronous request.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
Martin Honnen wrote:
[...]
If you are using XMLHttpRequest (Mozilla, Netscape, latest Safari and
Opera) or Microsoft.XMLHTTP (IE/Win) then you can however use the third
argument to the open method set to false to have a synchronous request.


My current RPC system is a home-grown thing based around persistent
connections; it works scarily well, and doesn't appear to actually break
any standards (although they get bent quite a lot).

How portable is XMLHttpRequest? If I were to switch to using that, which
would be rather simpler, how badly am I going to restrict myself in browser
choice?

--
+- David Given --McQ-+
| dg@cowlark.com | "Anything that makes people that angry is worth
| (dg@tao-group.com) | doing again." --- Scott Adams
+- www.cowlark.com --+

Jul 23 '05 #4


David Given wrote:

My current RPC system is a home-grown thing based around persistent
connections; it works scarily well, and doesn't appear to actually break
any standards (although they get bent quite a lot).
Then what are you doing, loading stuff into a hidden iframe that calls
back into the parent window?
How portable is XMLHttpRequest? If I were to switch to using that, which
would be rather simpler, how badly am I going to restrict myself in browser
choice?


It is supported in Mozilla 1.0 and later (well at least 1.0 and later,
supported for that started in earlier Mozilla releases but I can't tell
the exact version) meaning Netscape 7.xy have it, Firefox releases and
other Mozilla based browsers.
IE on Windows has Microsoft.XMLHTTP since IE 5.
Recent Safari and Opera versions have XMLHttpRequest too, I think Safari
since 1.2, Opera so far only in the 7.6x preview and 8.00 beta releases.
Jim in
<http://jibbering.com/2002/4/httprequest.html>
also suggests that Konqueror has support for XMLHttpRequest but I have
not tested that so far and do not currently know which Konqueror
versions support that.
And Opera for instance appears at least currently not to focus on being
compatible to Mozilla's XMLHttpRequest (which was designed to be
compatible to Microsoft.XMLHTTP) but to have enough functionality that
GMAIL works with Opera. So even in the Opera 8.00 beta methods like
setRequestHeader or getAllResponseHeaders are missing.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5
Martin Honnen wrote:
[...]
Then what are you doing, loading stuff into a hidden iframe that calls
back into the parent window?
Yup. Been done, has it?

[...] It is supported in...


It sounds like it either *is* supported on all browsers that have the
features I need, or *will be* supported, so I'll switch to using that ---
much more straightforward.

One thing I haven't been able to determine is whether it's possible to make
multiple requests on the same session, using HTTP pipelining. The
documentation seems to be a bit fuzzy. If so, it'd be really handy. Even
handier would be the ability to send data asynchronously from the server to
the client; again, the documentation's fuzzy, but it seems like that'd be
possible.

I'm actually a little surprised that this isn't being used more...

--
+- David Given --McQ-+
| dg@cowlark.com | Uglúk u bagronk sha pushdug Internet-glob búbhosh
| (dg@tao-group.com) | skai.
+- www.cowlark.com --+

Jul 23 '05 #6

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

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
10
by: Dica | last post by:
i've used threads in a couple of c# desktop apps with no problems before, but can't seem to get this code to update my listBox: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
1
by: Nikolay Petrov | last post by:
Can I use threads ih ASP.NET page? I one to run a long operation in separate thread and from it to update a control in aps page to display status information. Is this possible? My first guess is...
10
by: VK | last post by:
Yet about the code posted at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bf33c7bbb9f699f9/0336cb59fb5920c5> I just noticed another thing I did not notice last year...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.