Fix a javascript race condition issue