通过 javascript 加载缓慢的外部文件时,有什么方法可以优雅地强制执行超时限制吗?

发布于 2024-08-16 18:41:22 字数 256 浏览 3 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

以酷 2024-08-23 18:41:22

几个问题:您可以使用 XMLHttpRequest (又名 ajax)使用超时阈值,但由于它位于 otherserver.com 上,因此您无法使用 XMLHttpRequest (并支持所有 A 级浏览器),因为同源策略限制

如果脚本引入了任何类型的全局名称(例如任何变量名称、函数名称等),您可以尝试 setTimeout 来继续检查它:

var TIMELIMIT = 5; // seconds until timeout
var start = new Date;

setTimeout(function() {
  // check for something introduced by your external script.
  // A variable, namespace or function name here is adequate:
  var scriptIncluded = 'otherServerVariable' in window;

  if(!scriptIncluded) {
    if ((new Date - start) / 1000 >= TIMELIMIT) {
      // timed out
      alert("Please try again")
    }
    else {
      // keep waiting...
      setTimeout(arguments.callee, 100)
    }
  }
}, 100)

我看到的问题是您无法取消对脚本。如果我错了,请纠正我,但从 DOM 中删除

我想你可能不走运。

Couple issues: you can use timeout thresholds with XMLHttpRequest (aka ajax), but then since it's on an otherserver.com you cannot use XMLHttpRequest (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:

var TIMELIMIT = 5; // seconds until timeout
var start = new Date;

setTimeout(function() {
  // check for something introduced by your external script.
  // A variable, namespace or function name here is adequate:
  var scriptIncluded = 'otherServerVariable' in window;

  if(!scriptIncluded) {
    if ((new Date - start) / 1000 >= TIMELIMIT) {
      // timed out
      alert("Please try again")
    }
    else {
      // keep waiting...
      setTimeout(arguments.callee, 100)
    }
  }
}, 100)

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.

挖鼻大婶 2024-08-23 18:41:22

我能想到的唯一方法是在另一个(启用 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).

嘿哥们儿 2024-08-23 18:41:22

这纯粹是理论上的:

我想你可以声明一个全局变量 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 true hadLoaded=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.

对风讲故事 2024-08-23 18:41:22

我认为唯一的方法是通过 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.

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