取消加载缓慢的远程 JS 调用的最佳方法

发布于 2024-07-09 06:42:26 字数 151 浏览 5 评论 0原文

我有一个远程JS,它必须出现在文档的头部。 如果服务器响应缓慢或无法访问,显然这会减慢或阻止页面加载。 我一直在寻找一种简单的方法来设置 3 秒(可能更少)的限制,让它放弃并简单地不加载功能。

有没有人有一个简单的方法只用 JavaScript 来做到这一点?

I have a remote JS that must appear in the head of the document. If the server is slow to respond or inaccessible, obviously this slows or prevents the page from loading. I have been searching for a simple way to set a limit of say 3 seconds (probably less) for it to give up and simply not load the functionality.

Does anyone have a simple way to do this with Javascript only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-07-16 06:42:26

页面加载完成后包含 .JS 文件:

<script type='text/javascript'>
window.onload = function(){
 document.write("<script type='text/javascript' src='http://domain.com/file.js'></script>");
}
</script>

将其放在文档的 HEAD 中,然后该文件将在页面加载完成后开始加载。

Include the .JS file after the page is done loading:

<script type='text/javascript'>
window.onload = function(){
 document.write("<script type='text/javascript' src='http://domain.com/file.js'></script>");
}
</script>

Place that in the HEAD of your document, then the file will start loading when the page has finished loading.

最近可好 2024-07-16 06:42:26

如果这是一个问题,请将 .js 分开,并在 onload="" 中包含低端 css 函数和慢速函数;

If that is an issue, split the .js apart and have the low end css functions in the and the slow ones in the onload="";

多情出卖 2024-07-16 06:42:26

如果 window.onload 事件调用对您来说太晚了,那么只需将您的 javascript 文件直接内联到页面中即可。

If window.onload event is invoked too late for you, then just inline your javascript file directly into the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文