title: JavaScript synchronization and critical sections in event handlers
body: <p>I have a function which is an event handler for <code>websocket.onmessage</code>, now since the server can send multiple messages (one after another) and each message will trigger that event, and since the function block may take a few seconds (a lot of rendering going on inside), the function may be called again while the first function call is still running. 
  I need a critical block in this function in some cases so that the second call will only start the critical section when the first call ends, what's considered a 'best practice' for implementing locks in JavaScript?</p>