Fixed javascript error on race condition