Tim and Everyone else that responded to this thread: THANKS!<br><br><br>I think tim hit is on the button with this: <br><br>"functions like setTimeout() and asynchronous callbacks need to wait for the script engine to sleep before they're able to run."<br>
<br>I have been futzing around with a sort of wrapper for making JSON requests and just needed to ensure that a certain section<br>of code was 'atomic'. Sounds like as long as i'm not calling alert() in the middle of it, nothing to worry about: phew!<br>
<br>Interesting also to note the possibilities with threading in HTML5. Sounds like things are going to get *complicated*.<br><br>Anyways, thanks!<br><br>Erik<br><br><div class="gmail_quote">On Mon, Nov 3, 2008 at 10:40 PM, Tim Schaub <span dir="ltr"><<a href="mailto:tschaub@opengeo.org">tschaub@opengeo.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hey-<br>
<div class="Ih2E3d"><br>
Erik Uzureau wrote:<br>
> Dear OpenLayers users & dev,<br>
><br>
> Pardon the non-geo post, but I am wondering if anyone out there has come<br>
> up across issues with<br>
> synchronicity in javascript?<br>
><br>
> After pouring over this stuff for several hours, I think I am pretty<br>
> resolved to just say "ok, javascript<br>
> is event-driven and not truly multithreaded, so there is no need to worry".<br>
><br>
> In the off-chance that someone out there might have information<br>
> otherwise, please let me know.<br>
<br>
</div>I think the clearest answer is that event listeners and "functions like<br>
setTimeout() and asynchronous callbacks need to wait for the script<br>
engine to sleep before they're able to run."<br>
<br>
Maybe you're concerned about something else, but if you're wondering<br>
whether a sequence of event handling code can be executed again before<br>
it has finished, the answer is no.<br>
<br>
Again, maybe you're reading up on mutexes for something else, but here's<br>
some code that demonstrates that you can't change the "current" value<br>
with subsequent triggers of the same event while the event handling code<br>
is executing, no matter how long it takes to run.<br>
<br>
function check(iter) {<br>
var current;<br>
document.body.onmousemove = function() {<br>
var start = (new Date).getTime();<br>
var id = start + Math.random();<br>
current = id;<br>
// do something that takes a while<br>
var a = [];<br>
for(var i=0; i<iter; ++i) {<br>
a.push(i);<br>
}<br>
var end = (new Date).getTime();<br>
console.log(<br>
id + " start: " + start + " end: " + end +<br>
" elapsed: " + (end - start)<br>
);<br>
if(current != id) {<br>
console.error(id + " failed");<br>
}<br>
}<br>
}<br>
<br>
// loop runs in <1ms, events fired every ~17ms<br>
check(1000); // wiggle mouse over body<br>
<br>
// loop runs in ~25ms, events fired every ~45ms<br>
check(100000);<br>
<br>
// loop runs in ~225ms, events fired every ~250ms<br>
check(100000);<br>
<br>
You can do the same sort of thing with setTimeout.<br>
<br>
Tim<br>
<div class="Ih2E3d"><br>
><br>
> Below are two of the links that have fed my brain for the last few hours:<br>
><br>
> debate:<br>
> <a href="http://stackoverflow.com/questions/124764/are-mutexes-needed-in-javascript" target="_blank">http://stackoverflow.com/questions/124764/are-mutexes-needed-in-javascript</a><br>
><br>
> implementation of a mutex in js:<br>
> <a href="http://www.developer.com/lang/jscript/article.php/3592016" target="_blank">http://www.developer.com/lang/jscript/article.php/3592016</a><br>
> (note that his actual code for this *only* runs in ie, which obviously<br>
> docks it a fair amount of credit)<br>
><br>
> cheers,<br>
> erik<br>
><br>
><br>
</div>> ------------------------------------------------------------------------<br>
<div class="Ih2E3d">><br>
> _______________________________________________<br>
> Dev mailing list<br>
> <a href="mailto:Dev@openlayers.org">Dev@openlayers.org</a><br>
> <a href="http://openlayers.org/mailman/listinfo/dev" target="_blank">http://openlayers.org/mailman/listinfo/dev</a><br>
<br>
<br>
</div><font color="#888888">--<br>
Tim Schaub<br>
OpenGeo - <a href="http://opengeo.org" target="_blank">http://opengeo.org</a><br>
Expert service straight from the developers.<br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<br>
Dev mailing list<br>
<a href="mailto:Dev@openlayers.org">Dev@openlayers.org</a><br>
<a href="http://openlayers.org/mailman/listinfo/dev" target="_blank">http://openlayers.org/mailman/listinfo/dev</a><br>
</div></div></blockquote></div><br>