iframe 加载时间限制使用 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17759103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:30:57  来源:igfitidea点击:

iframe loading time limit use javascript

javascriptiframesettimeout

提问by Biswajit Das

i need to stop loading my iframe page after 5000 ms i'm use these but it's refresh the iframe every 5000 ms later what the problem . pleas fix it pleas. thanks

我需要在 5000 毫秒后停止加载我的 iframe 页面,我正在使用这些页面,但每隔 5000 毫秒刷新一次 iframe 是什么问题。请修复它。谢谢

<iframe id="iframe1" src="" width="920" height="900" border="2"></iframe>
<script type="text/javascript"> 
function setIframeSrc() {
  var s = "http://lx5.in/CGIT-Results-p2-2013.php";
  var iframe1 = document.getElementById('iframe1');
  if ( -1 == navigator.userAgent.indexOf("MSIE") ) {
    iframe1.src = s;
setTimeout(setIframeSrc, 5000);
  }
  else {
    iframe1.location = s;
setTimeout(setIframeSrc, 5000);
  }
}
setTimeout(setIframeSrc, 5000);
</script>

回答by Stano

To stop iframe loading you can use the following code based on this answer:

要停止 iframe 加载,您可以根据此答案使用以下代码:

function setIframeSrc() {
  var s = "http://lx5.in/CGIT-Results-p2-2013.php";
  var iframe1 = document.getElementById('iframe1');
  iframe1.src = s;
  setTimeout(function(){
      if (window.stop) {
          window.stop();
      } else {
          document.execCommand('Stop'); // MSIE
      }
  }, 5000);
}
setTimeout(setIframeSrc, 5000);

jsfiddle/ jsfiddle/show

jsfiddle/ jsfiddle/显示

回答by Grzegorz

I would suggest rethinking the idea. IMO it should be the server that displays timeout information and not the client using some JavaScript timeouts. Explanation below.

我建议重新考虑这个想法。IMO 应该是显示超时信息的服务器,而不是使用某些 JavaScript 超时的客户端。下面解释。

Your code just says: refresh the iframe every 5 seconds.

您的代码只是说:每 5 秒刷新一次 iframe。

You cannot stop iframe request execution. You can change iframe's src location or remove it from the DOM tree. But the request that is started is unstoppable once it reaches the server.

您无法停止 iframe 请求的执行。您可以更改 iframe 的 src 位置或将其从 DOM 树中删除。但是启动的请求一旦到达服务器就不可阻挡。

So:

所以:

<iframe id="iframe1" src="{url to query}" ...>

and in the script:

并在脚本中:

function displayTimeout() {
 (...)
 var p = iframe1.parentNode;
 p.removeChild(iframe1);
 var div = document.createElement("div");
 var text = document.createTextNode("Timeout loading iframe");
 div.appendChild(text);
 p.appendChild(div);
}
setTimeout(displayTimeout,5000);

But this solution has serious drawbacks- in order not to display timeout when iframe manages to load in time you should cancel the timeout. But how to do that?

但是这个解决方案有严重的缺点- 为了在 iframe 设法及时加载时不显示超时,您应该取消超时。但是怎么做呢?

If you want to do it from iframe - see iframe accessing parent DOM?

如果您想从 iframe 执行此操作 - 请参阅iframe 访问父 DOM?

If from parent element - see jQuery/JavaScript: accessing contents of an iframe

如果来自父元素 - 请参阅jQuery/JavaScript:访问 iframe 的内容

The further you go, the more problems and the more complicated the solution becomes. So I would suggest abandoning it.

你走得越远,问题就越多,解决方案也就越复杂。所以我建议放弃它。