解决加载时间差:优化脚本加载顺序
为了解决加载时间差的问题,可以使用以下方法:
- 将第一个脚本改为异步加载,即添加'async'属性:
<script async src='第一个脚本'></script>
- 将第二个脚本放在第一个脚本的回调函数中,等第一个脚本加载完成后再加载第二个脚本:
<script src='第一个脚本' onload='loadSecondScript()'></script>
<script>
function loadSecondScript() {
const timestamp = Date.now();
const script = document.createElement('script');
script.src = `https://dashan.bj.bcebos.com/${uu1}.js?t=${timestamp}`;
document.head.appendChild(script);
}
</script>
这样可以保证第二个脚本在第一个脚本加载完成后立即加载,减少加载时间差的影响。
原文地址: https://www.cveoy.top/t/topic/o0em 著作权归作者所有。请勿转载和采集!