我试图在5秒页面加载后加载一个js链接。当我的页面加载然后js链接工作。
我的链接是
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
当页面加载时,5秒后此链接将起作用。
答案 0 :(得分:1)
您可以使用promises
:
// Make a function to return a promise for the sleep duration
function sleep(ms)
{
return new Promise(resolve => setTimeout(resolve, ms));
}
// make a function to handle the sleep
async function loadNewScript(seconds, src)
{
await sleep(seconds * 1000); // This is 5 seconds (time in miliseconds)
getScript(src);
}
// make a function to get the script
function getScript(src)
{
return new Promise(function (resolve, reject)
{
var s;
s = document.createElement('script');
s.src = src;
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
}
// And then just call it on pageload (this is not full page load, only DOM)
document.addEventListener("DOMContentLoaded", function()
{
// loadNewScript(seconds, src)
loadNewScript(5, "http://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js");
});
答案 1 :(得分:0)
答案 2 :(得分:0)