JavaScript: function not working without alert()

The alert() function blocks the code execution and delays the processing of the javascript code on bottom of it. This could give time to the ajax request to complete.


This is not an answer but a debugging tip (can't do proper code formatting in a comment).

Add these two calls after the alert():

console.log( "errors:", errors );
console.log( "hidden:", $('#idOfTheHiddenField')[0] );

Leave the alert commented out and run it. What does it show in the developer console?

Then for comparison, uncomment the alert, so it runs before those two calls, and run it in Firefox. Now what is in the console?


Thanks to everyone's answers, and gvee's answer in particular, I managed to get it work. Obviously, there was indeed something asynchronous going on in the randomFunctionToReloadThePage(), and I managed to fix it using the .ajaxStop() method.

Edit: a second bug was found while using ajaxStop(). The solution I found to be working properly is as follows:

1) Add an extra parameter to the randomFunctionToReloadThePage

2) Invoke the function with that extra parameter in the respective page.