JavaScript命令在Safari中不按顺序执行 [英] JavaScript commands not executed in order in Safari

查看:88
本文介绍了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.

这是小提琴:

http://jsfiddle.net / akJD7 /

任何人都可以确认他们看到了这个,如果有,请告诉我为什么会发生这种情况?

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);

这篇关于JavaScript命令在Safari中不按顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆