Javascript not being executed sequentially
===

Recently I have been having problems with javascript functions that are not being executed in order, and I just can't understand why this is the case. For example, I had a javascript function which would handle the creation of a zip folder in the following way:

 1. Show loading icon
 2. Make ajax call to PHP to zip up files
 3. Handle ajax response (Success/Failure)
 4. Hide loading icon
 5. Download zip file

This series of steps worked absolutely fine in FireFox, but in Chrome and IE the code wasn't executed sequentially - the code was executed as step 2, 3, 4, 1, 5. For some reason the ajax call was being executed first, despite not being the first line of code within the function. Can anyone explain why this would be the case?

In the end, the only work around I found was to put steps 2-5 inside a separate function and call it via setTimeout().

    