JavaScript命令在Safari中不按顺序执行
我在处理另一个问题时发现了这个错误。列出的JavaScript命令的顺序与它们在Safari中执行的顺序不同:
I discovered this bug while dealing with another issue. The order of JavaScript commands listed is different than the order of their execution in Safari:
示例:
alert('here');
document.write('This is the hidden message.');
alert('You should be seeing the hidden message by now.');
在我的浏览器中,警告
在执行之前执行 document.write()
语句。我在使用Safari版本5.17,6.0和6.0.2的两个不同Mac OS X上看到了这个错误,但我还没有确认其他人已经看过这个。
In my browser the alerts
are executed before the document.write()
statement. I've seen this bug on two different Mac OS X's using Safari versions 5.17, 6.0 and 6.0.2, but I haven't confirmed that anyone else has seen this yet.
这是小提琴:
任何人都可以确认他们看到了这个,如果有,请告诉我为什么会发生这种情况?
Can anyone confirm that they see this and if so, tell me why this is happening?
严格来说,我认为这不是一个错误。只是它是全部同步的,并且在第二次警报之前没有重新绘制。重绘通常不会发生在浏览器事件循环的相同滴答内(尽管 document.write
似乎强制在其他浏览器中重新绘制,例如Chrome)。
I don't think that's a bug, strictly speaking. It's just that it's all synchronous, and there was no repaint before the second alert. Repaints don't usually happen within the same "tick" of the browser's event loop (although document.write
seems to force a repaint in other browsers, e.g. Chrome).
这个(丑陋的)解决方法应该修复它:
This (ugly) workaround should fix it:
alert('here');
document.write('This is the hidden message.');
setTimeout(function() {
alert('You should be seeing the hidden message by now.');
}, 0);