[Javascript] atomic operations

Scott Reynen scott at randomchaos.com
Wed May 3 10:00:03 CDT 2006


On May 3, 2006, at 9:00 AM, Andrew Clover wrote:

>> So multiple events, timeouts etc. will all be scheduled for execution
>> one after the other, and they will all be performed in their entirety
>> before the next one will start executing.
>
> This makes perfect sense and *should* be what happens. Except, it  
> isn't always.
>
> From bitter experience I know of two particular cases where it does  
> not:
>
>  - In Firefox 1.5 (at least; probably all Mozilla, possibly other  
> browsers), when you call a browser dialogue function such as  
> window.alert(). At this point other event handlers/timeouts can  
> fire and their code will execute in the middle of the execution of  
> the alert()ing script block.
>
>  - In IE with Java (at least Sun Java 1.4 on IE6, probably all IE 
> +Sun Java; does not affect MS JVM), when you call any method (or  
> access any property) on an applet from JavaScript, other event  
> handlers/timeouts can fire and their code will execute in the  
> middle of the execution of the applet-calling code. This is  
> especially pernicious where multiple threads of script are trying  
> to silently call an applet, as totally random intermittant faults  
> can result.

In both cases, the event is a call to a process outside of  
JavaScript.  Alerts call the browser's window manager and then the  
event is over until the window manager initiates a new event with a  
response.  Applet calls call the applet and then the event is over  
until the applet creates a new event with a response.  So these are  
both actually *two* events, one call and one response, which work  
exactly as David stated above.  The ability to launch external  
threads doesn't make JavaScript multi-threaded.  There's still only  
one thread of *JavaScript events*.

Also, I'd say that's not what *should* happen.  When I set a timeout  
for 10 seconds, it would ideally execute in exactly ten seconds, not  
ten seconds plus whatever time it takes to clear the event queue.

Peace,
Scott



More information about the Javascript mailing list