通过 javascript 加载缓慢的外部文件时,有什么方法可以优雅地强制执行超时限制吗?
我正在使用 javascript 包含从另一台服务器上的 php 文件提供的一些内容。然而,其他服务有时可能会变得不稳定,要么需要很长时间才能加载,要么根本无法加载。
JS 有没有办法在失败并显示“请重试”消息之前尝试获取外部数据 x 秒?
<script type="text/javascript" src="htp://otherserver.com/myscript.php"></script>
I'm using javascript to include some content served up from a php file on another server. However, this other service can sometimes get flaky and either take a long time to load or will not load at all.
Is there a way in JS to try to get the external data for x number of seconds before failing and displaying a "please try again" message?
<script type="text/javascript" src="htp://otherserver.com/myscript.php"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几个问题:您可以使用
XMLHttpRequest
(又名 ajax)使用超时阈值,但由于它位于otherserver.com
上,因此您无法使用XMLHttpRequest
(并支持所有 A 级浏览器),因为同源策略限制。如果脚本引入了任何类型的全局名称(例如任何变量名称、函数名称等),您可以尝试 setTimeout 来继续检查它:
我看到的问题是您无法取消对脚本。如果我错了,请纠正我,但从 DOM 中删除
仍会使浏览器对资源的请求保持活动状态。因此,尽管您可以检测脚本加载时间超过 x 秒,但您无法取消该请求。
我想你可能不走运。
Couple issues: you can use timeout thresholds with
XMLHttpRequest
(aka ajax), but then since it's on anotherserver.com
you cannot useXMLHttpRequest
(and support all A-grade browsers) due to the Same Origin Policy restriction.If the script introduces any kind of global name (eg any variable name, function name, etc) You can try
setTimeout
to keep checking for it:The problem as I see it is you cannot cancel the request for the script. Please someone correct me if I'm wrong but removing the
<script>
from the DOM will still leave the browser's request for the resource active. So although you can detect that the script is taking longer than x seconds to load, you can't cancel the request.I think you may be out of luck.
我能想到的唯一方法是在另一个(启用 PHP 的)服务器上创建一个代理,该服务器将为您获取数据,但在达到一定的超时限制时会停止(并且它只能返回一个空结果)。
The only way I can think of doing this is to create a proxy on another (PHP-enabled) server which will fetch the data for you, but will stop when a certain timeout limit has been reached (and it can just return an empty result).
这纯粹是理论上的:
标签可以动态插入到 DOM 中,此时脚本将被获取并处理。这种动态脚本标签注入就是一些实现跨域“AJAX”的方法。
我想你可以声明一个全局变量 var hasLoaded = false; 。在尝试加载的脚本末尾,您可以将该变量设置为 true
hadLoaded=true;
。将脚本标签注入 DOM 后,您可以启动 setTimeout(),其回调函数检查“hasLoaded”是否设置为 true。如果不是,您可以假设脚本尚未完全加载到浏览器中。如果有,您可以假设它已完全加载。再说一次,这是理论上的,但如果你测试它,一定要报告回来,我很好奇。
This is purely, purely theoretical:
<script>
tags can be dynamically inserted into the DOM, at which point the script will be fetched and processed. This dynamic script tag injection is how some achieve cross-domain "AJAX."I would imagine you could declare a global variable
var hasLoaded = false;
. At the end of the script you are attempting to load you could set that variable to truehadLoaded=true;
. After injecting the script tag into the DOM you could then kickoff a setTimeout() whose callback function checks to see if "hasLoaded" is set to true. If it isn't, you can assume the script has not yet loaded fully into the browser. If it has, you can assume it has loaded completely.Again, this is theoretical, but if you test it be sure to report back, I'm very curious.
我认为唯一的方法是通过 ajax 获取文件的内容,然后设置一个计时器。如果请求在计时器之前完成,您可以使用 eval 评估响应(无论如何这不是更好的解决方案),否则您可以停止 ajax 请求并写入错误消息。
I think that the only way to do this is take the content of the file via ajax and then set a timer. If the request finishes before the timer you can evaluate the respons with eval(that's not the better solution anyway), otherwise you can stop the ajax request and write the error message.