Chrome 的窗口调整大小事件问题

发布于 2024-09-09 04:08:01 字数 562 浏览 0 评论 0原文

当窗口大小调整时,我正在尝试处理网站上的内容,但我遇到了一个无法解决的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-09-16 04:08:01

这并不是单线程与多线程的区别,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 with alert() calls...which are really bad for judging threading or race conditions, since they directly interfere and change them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文