javascript - Chrome - alert() 的 Javascript 执行顺序问题
问题描述
今天我想知道 Chrome 中 JavaScript 函数“alert()”的执行顺序
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test1">
test1 test1 test1 test1
</div>
<div id="test2">
test2 test2 test2 test2
</div>
<input type="button" onclick="runTest()" value="start test">
<script>
function runTest()
{
$('#test1').css('background-color', 'red');
alert('test1');
$('#test2').css('background-color', 'red');
alert('test2');
}
</script>
在 Firefox 62 和 IE11 中(按预期工作):
1) BGcolor Line 1
2) Alertbox 1
3) (click OK)
4) BGcolor Line 2
5) Alertbox 2
6) (click OK)
在 Chrome 69.0.3497.100 中:
1) Alertbox 1
2) (click OK)
3) Alertbox 2
4) (click OK)
5) BGcolor Line 1
6) BGcolor Line 2
为什么 Chrome 不启动 alert() 并等待点击 OK?
谢谢奥利_
解决方案
通过对类似问题进行一些研究,这个问题的答案似乎是正确的:
...样式对象的更改会向页面渲染器触发重绘请求消息,并且在浏览器空闲时立即处理,即在此脚本例程结束之后。
显然,只有 Chrome 会这样做。
推荐阅读
- django - 从 Django Rest Framework 返回 XML、JSON 和 PDF
- ruby-on-rails - 捆绑安装失败但在使用 capistrano deploy 时仍然停止 sidekiq
- sql - 使用实体框架插入大量重复数据
- python - 如何在 Pandas 中连接包含列表(系列)的两列
- azure-devops - VSTS 配置代理池和队列
- linux - 本地针对远程文件的 Ag 命令
- arrays - 如何在打字稿中定义一个常量数组
- reactjs - 在知道 id 时添加 refs 以响应组件
- javascript - 研究什么来解决这类问题?
- php - WP 站点某些 url 重定向到 WP 中的外部 URL