Re: Asynchronous Events

Svend Tofte <[email protected]>
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
Maybe the engine runs a single thread of execution, and traps to a
handler, when events interrupts, but who knows (well someone of
course).

I could imagine, that each instance of the JS engine, runs a new
thread, for each singular page. As only a single thread lives, to
actually execute JS, event handlers, which are initiated by the
browser (different thread from JS engine), the caller may enter any
number of mutexes/blocks, until the JS engine thread starts to idle
(indicating it is done doing whatever it was doing).

Consider if this was not the case, and JS was truly async, you'd have
to write mutexes and other fun stuff, if you could not be sure, that
only a single thing in JS, would ever be running.

I'm not sure of this, but I also don't see any thread creation in the
SpiderMonkey code (you can start the trail from
http://lxr.mozilla.org/seamonkey/source/dom/src/events/nsJSEventListener.cpp
and go from there).

I've often seen referrals to "light-weight" threads in reference to
JS, but I'm not sure what is meant by that.

You can also get a very real indication of this, by the following:

<html>
<script>
function grind(s) {
    var dnow = new Date();
    var r = 900000;
    while (r--) Math.random();
    var dend = new Date();
    var f = document.getElementById("foo");
    f.innerHTML += s + "<br>" + dnow.getTime() + "<br>" +
dend.getTime() + "<hr>";
}
setTimeout("grind('a');",10);
setTimeout("grind('b');",10);
</script>
<div id="foo"></div>
</html>

And just remember that setTimeout is for all intents and purposes the
same as an event.

Maybe other list readers can be more informative, instead of vaguely
confirming your suspecions :)

Regards
Svend

On 11/1/05, Marco <[email protected]> wrote:
> Should events be asynchronous in javascript implementations? They seem
> not to be.
>
> Consider the following example. A page has two multiple-select boxes,
> the one currently selected by the user is stored in the variable
> window.selectedSelect and maybe highlighted with a different
> background color.
>
> Do a handle(s) upon a click on the s select where
>
> function handle(s) {
>        if (!window.selectedSelect || window.selectedSelect != s) {
>                window.selectedSelect = s;
>                setTimeout(function () {alert("hello");}, 2000);
>                while(true);
>        } else {
>                alert("second selection");
>        }
> }
>
> Now click on the first select, and than quickly click on the second
> one. In a few seconds the browser will complain about slow execution.
>
> In both Firefox and IE:
> - alert("hello") is executed only when the browser stops the while(true) loop
> - alert("second selection") is not executed upon the second click,
> while the first event handler is doing the while(true) foo.
>
> It seems that the second event is served only after the first one, not
> simultaneously.
>
> Bye,
> Marco
>
>
>
> Unsubscribe
> [email protected]
>
> List info
> http://www.quirksmode.org/dom/list.html
> Yahoo! Groups Links
>
>
>
>
>
>
>
>


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page
http://us.click.yahoo.com/dpRU5A/wUILAA/yQLSAA/9rHolB/TM
--------------------------------------------------------------------~-> 

Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.