开发者

Synchronize an array with javascript

开发者 https://www.devze.com 2023-01-05 08:59 出处:网络
I wonder if there is a way to synchronize objects/methods in JavaScript in a similar way that you can do it in Java. I am developing an interface for the new WebSocket in html5 and need a way to match

I wonder if there is a way to synchronize objects/methods in JavaScript in a similar way that you can 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. Therefore I'm saving the requests (with a 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 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!

开发者_StackOverflow中文版

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.


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?).

0

精彩评论

暂无评论...
验证码 换一张
取 消