A Mutex locking system non-blocking for Javascript
Download the library and include it in your html.
<script src="js/mutex.js"></script>
Then you just need to create an instance of the class and use it this way.
<script>
var mutex = new Mutex();
mutex.lock(function(){
/* The code to process goes here */
console.log('Processing the code');
/* When the processing is finished */
mutex.unlock();
});
</script>