取消加载缓慢的远程 JS 调用的最佳方法
我有一个远程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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
页面加载完成后包含 .JS 文件:
将其放在文档的 HEAD 中,然后该文件将在页面加载完成后开始加载。
Include the .JS file after the page is done loading:
Place that in the HEAD of your document, then the file will start loading when the page has finished loading.
如果这是一个问题,请将 .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="";
如果 window.onload 事件调用对您来说太晚了,那么只需将您的 javascript 文件直接内联到页面中即可。
If window.onload event is invoked too late for you, then just inline your javascript file directly into the page.