Chrome 的窗口调整大小事件问题
当窗口大小调整时,我正在尝试处理网站上的内容,但我遇到了一个无法解决的 Chrome 问题。这是代码,非常简化:
<script language="javascript" type="text/javascript">
window.onresize = function()
{
alert(1);
alert(2);
alert(3);
alert(4);
};
});
</script>
当我单击“恢复”窗口(make 更小)时,它工作正常(由于某种原因执行两次)。最大化窗口时,警报按以下顺序排列:1、2、1、2、3、4、3、4;或者有时是 1, 1, 2, 3, 4, 2, 3, 4。在 IE 中似乎工作得很好(执行三次),而 FF 则很好(只执行一次)。我也尝试过使用 $(window).resize(...); 的 jQuery 等效项。得到相同的结果。我知道 javascript 不是多线程的,但在这种情况下看起来几乎是多线程的。有人有什么想法吗?
I'm trying to handle stuff on my site when the window is resized and I have ran into an issue with Chrome that I can't figure out. Here is the code, very simplified:
<script language="javascript" type="text/javascript">
window.onresize = function()
{
alert(1);
alert(2);
alert(3);
alert(4);
};
});
</script>
When I click to Restore the window (make is smaller), it works fine (executing twice for some reason). When maximizing the window, the alerts go in this order: 1, 2, 1, 2, 3, 4, 3, 4; or sometimes 1, 1, 2, 3, 4, 2, 3, 4. In IE it seems to work fine (executing three times) and FF is fine (executing only once). I've also tried this with the jQuery equivalent of $(window).resize(...); with the same result. I know that javascript is not multi-threaded, but it almost seems like it is with this situation. Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是单线程与多线程的区别,
alert()
调用是锁定执行线程的少数事情之一,因此当警报启动时,其他事情在后台排队。如果您没有使用警报,而是执行了
console.log()
或写入页面进行调试,那么您将获得比使用alert()
更加一致的行为code> 调用...这对于判断线程或竞争条件来说确实很糟糕,因为它们直接干扰和改变它们。It's not so much single vs multi-threaded, an
alert()
call is one of the few things that locks up the execution thread, so while an alert is up, other things are queuing in the background.If you didn't use alerts and instead did a
console.log()
or wrote into the page for your debugging, you'll get much more consistent behavior than withalert()
calls...which are really bad for judging threading or race conditions, since they directly interfere and change them.