views:

18

answers:

1

Hello, I wonder if there is a way to synchronize objects/methods in JavaScript in a similar way that you do it in Java. I am developing an interface for the new WebSocket in html5 and need a way to match outgoing requests with incoming responses. Therefor I'm saving the requests (with an unique id) in an array on the client side and then I iterate through the array when I receive a response looking for the matching request.

A problem is that might occur on the client side is if I have multiple timers that are making requests to the server independently of each other. If a the request function is inserting a "request-reference" into the array at the same time as the respond-listener is iterating through the array it's bound to break!

So how do I solve this problem? My initial thoughts was to simply synchronize the array as one could have done in Java (putting a lock on the object and force the other functions to wait) but I have found no syntax of how I would do this in JavaScript.

+3  A: 

Javascript runs in a single thread in the browser, so there is no need to synchronize.

See here for details. See this SO question and answers as well (Why doesn’t JavaScript support multithreading?).

Oded
unless you use workers : see https://developer.mozilla.org/en/Using_web_workers but even then you have to work really hard to cause problems in your code
Redlab