At a guess I'd say the issue here is to do with using 'setTimeout', I reckon that the tasks are being queued so quickly that they are not executing in the correct order and thus causing the JS parser to get 'confused'. When you put alerts in, it is causing a gap between when each 'setTimeout'...