gpt4 book ai didi

javascript - setTimeout - 奇怪的行为

转载 作者:数据小太阳 更新时间:2023-10-29 05:36:12 24 4
gpt4 key购买 nike

考虑以下 HTML:

<html>
<head></head>
<body>
<input type="text" onblur="window.setTimeout('document.title += 2;', 0);" />
<input type="button" onclick="document.title += 1" />
</body>
</html>

[ Demo with 0 delay , 100ms delay , 150ms delay ]

以及以下步骤:

  • 用户输入(焦点)。
  • 用户点击按钮。

现在,事件将按以下顺序发生:

  • 输入文本模糊事件。
  • 按钮点击事件。

在我得到的所有可用浏览器上测试它:

document.title = '21' //Expected behavior

但是!在生产浏览器 (Windows XP + IE 7) 上,我得到:

document.title = '12' //Unexpected behavior

我还尝试在我的本地计算机 (IE 10) 上以 IE 7 模式模拟它,但无法重现它。

这显然是我遇到的问题的简化示例。否则我可以简单地摆脱 setTimeout。

在真实场景中,setTimeout 调用实际上是由第三方脚本库(ASP.NET Dev Express 组件)进行的。

除了这个问题的实际解决方案(我认为我可以处理)之外,还有什么解释可以应用于此行为?

更新:

使用表达式new Date().getTime() 获取浏览器执行的每个步骤的时间。它发生如下:

1387369361417 //document.title += 1
1387369361433 //document.title += 2

最佳答案

两种可能性:

  1. 您的点击(鼠标按下 + 鼠标弹起)在 IE7 的最短超时期限之前完成。
  2. mousedown 状态是阻止脚本。事件必须等到其他脚本和用户交互完成后才会触发。考虑到 IE 中脚本 UI 怪异/可怕的历史,我敢打赌 mousedown “开始用户交互”,mouesup “结束用户交互”。在 IE7 中加载:

    <input type="text" onblur="window.setTimeout('output(2));', 0); output(3);" />
    <input type="button" onclick="output(1);" />

    http://jsfiddle.net/sMcE3/

    … 在您将焦点 () 放在文本字段上之后,单击该按钮非常缓慢。我猜您会看到 312。 (与任何一般浏览器都会显示的 321 不同。)

关于javascript - setTimeout - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com