延迟脚本加载 [英] Delay script loading
本文介绍了延迟脚本加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,如果我有以下内容:
So if I have the following:
<script type="text/javascript" src="offsite file I am referencing"></script>
我只是想延迟使用settimeout调用该文件的执行,我该怎么做呢?
and I simply want to delay the execution of calling that file using settimeout, how would I go about that?
非常奇怪的是,在一个简单的函数上使用settimeout我没有问题,但我对这个看似更简单的情况感到很难过。
Very strange in that I would have no problem using settimeout on a simple function, but I am kind of stumped in this seemingly more simple situation.
我的想法是我可以制作一个在x时间后调用该文件的函数,但调用函数中的文件似乎是在逃避我。
My thought would be I could just make a function that calls that file after x amount of time, but calling the file in the function seems to be escaping me.
推荐答案
你几乎就在那里。
执行以下操作:
var script = document.createElement('script');
script.src = "http://whatever.com/the/script.js";
document.getElementsByTagName('head')[0].appendChild(script);
这篇关于延迟脚本加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文