javascript - 加载外部 JS 并在同步后立即运行脚本
问题描述
我有这个 index.html:
<!DOCTYPE html>
<html>
<head>
<script src="src/index.js"></script>
<script>
alert("2");
</script>
</head>
<body>
</body>
</html>
这个 index.js:
alert("1");
怎么alert('2')
之前出现了alert('1')
。
据我所知,加载src/index.js
应该是一个阻塞操作。
https://codesandbox.io/s/kww2o7rm0v
谢谢
解决方案
您可以使用 SetTimeout() 函数开始第二个警报,或者在退出警报时调用另一个脚本中的函数,使用:
alert("1");
// You could either put all of the other script in a function and call that here, or change the src of the script
document.GetElementById("ScriptID").src = "src/index.js";
希望我能帮上忙!
推荐阅读
- php - Wordpress ajax 仅在包含类别帖子的页面上重复
- python-3.x - 'int' 对象在 lambda 函数内不可订阅
- java - 或者查询不支持spring数据dynamodb
- css - SVG 背景图像顶部有奇怪的线条
- scala - 在播放框架中为复选框动态生成标签
- r - 使用带有变量的 grepl
- linux - 为什么用我自己的 deb 自定义 sources.list 总是找不到 Ubuntu 的 deb
- twitter-bootstrap - Bootstrap 4,不同内容宽度的列之间的间距相等
- python - 无法使用 Pip 安装更新 Python 库
- android - 领域 SyncConfiguration.Builder 已弃用