Yes I’m aware that asking for a formal memory model in Javascript is a hopeless undertaking, so I’m settling for “All browsers follow these rules” or something.
My problem is the following: I have to send events in a defined interval to a server, but events may be added to my array while doing so, i.e.:
function storeEvent(event) {
// may be called at any time
storedEvents.push(event);
}
function broadcastEvents() {
if (storedEvents.length !== 0) {
var eventString = JSON.stringify(storedEvents);
storedEvents = [];
// send eventString to server
}
window.setTimeout(broadcastEvents, BROADCAST_TIMER);
}
There’s an obvious race condition in there and not even think of the missing memory barriers.
What to do? Really missing the Java memory model here..
There isn’t any race condition.
All JavaScript code in the browser is single-threaded.
The
setTimeoutcallback will run on the UI thread while it isn’t doing anything else.